Apache add "?" at the end - mod-rewrite

I want to rewrite domain/login to domain/login?
When I try RewriteRule ^/login https://%{SERVER_NAME}/login? I get too many redirects.
When I try RewriteRule ^/login/ https://%{SERVER_NAME}/login? nothing changes.
Any ideas what I'm doing wrong?

Related

Get values from wildcard sub-domain and the url using mod_rewrite

I have tried for many days to solve my need. Try to find in google and read at the stackoverflow, but I stil unable to get my answer.
I need to get the subdomain value including the value behind it.
for example:
subdomain_name.domain.com -> will execute domain.com/user.php?subdomain_value=subdomain_name
subdomain_name.domain.com/product_1.html --> will open page domain.com/user.php?page=1&subdomain_value=subdomain_name
I have tried to use code below:
RewriteCond %{HTTP_HOST} ^((?!www\.)[^.]+)\.domain\.com$
RewriteCond %{REQUEST_URI} !^/user\.php$ [NC]
RewriteRule ^(.*)$ /user.php?subdomain_value=%1
RewriteRule ^product_([0-9]+).html$ /user.php?process=list_produk&page=$1&subdomain=%1
But it failed. When try to open subdomain_name.domain.com/product_1.html, it will keep opening the content of subdomain_name.domain.com
If I removed the code:
RewriteRule ^(.*)$ /user.php?subdomain_value=%1
Then, subdomain_name.domain.com/product_1.html will open the correct page, but the address subdomain_name.domain.com fail to open the correct page.
so, How can I make all of the .htaccess code work fine? I have think very hard and try all possibilities, including added [L], [NC,L], [L,QSA], but all of them failed.
Please help.
Thanks.

Redirect not working for matching a pattern in my url

I am trying to redirect a pattern of urls
http://style.com/style-blog/entry/what-im-looking-for-in-my-next-15-inch-laptop
to
http://style.com/blog/entry/what-im-looking-for-in-my-next-15-inch-laptop
I have tried to match the "style-blog" here
^style-blog/([A-Za-z0-9-]+)$ or ^style-blog/$
I needed to the first version to get ANY remaining part of the url to append to the new url here
RewriteRule ^style-blog/([A-Za-z0-9-]+)$ http://style.com/you-blog/$1 [NC,L]
Thanks for pointers on what I am doing wrong.
I think you may just be missing a /:
RewriteRule ^/style-(blog/[A-Za-z0-9-]+)$ http://style-review.com/$2
Using wildcard:
RewriteRule ^/style-(blog/.*)$ http://style-review.com/$2
Do you even need the full URL?
RewriteRule ^/style-(blog/.*)$ /$2
Are the ^ and $ required? Wouldn't just this work?
RewriteRule style-blog/ blog/
RewriteRule ^style-blog/(.*)/(.*)$ http://%{HTTP_HOST}/you-blog/$1/$2 [R=301,L]
This appears to be working but thanks to you guys you got me on the right track and probably you didn't have enough information to solve it. Its not fully tested yet and might not cope with any extra 'folders' (bits between slashes)

Rewrite Query String with .htaccess

I'm trying to do a very simple rewrite of a query string
http://www.example.com/library.php?q=abscessed-tooth
to
http://www.example.com/library/abscessed-tooth
This is the code that I've written in my .htaccess file and it is doing nothing
RewriteEngine On
RewriteRule ^/library/?([^/]*)/?\/http://www.example.com/library.php?q=$1 [L]
Maybe likely .htaccess files are not considered in your environment. If in doubt turn on RewriteLogging as it is explained in the excellent documentation of the rewriting module.
Oh, and check the error log, you have a syntax error in the RewriteRule anyway: RewriteRule takes 2 arguments plus flags, your rule has only a single argument:
RewriteEngine On
RewriteRule ^library/([^/]*) http://www.example.com/library.php?q=$1 [L]
You need dollar sign in the end of "left" part not question mark:
^/library/([^/]*)/$ http://www.example.com/library.php?q=$1 [L]
Also do you need the question mark between / and ( ? It doesn't look like lookahead or lookbehind?
Try without wrapping slashes as well
^library/([^/]*)$ http://www.example.com/library.php?q=$1 [L]

mod_rewrite for pretty url with uriencoded query strings

ok, so I've tried many things I've found on SO and elsewhere, but I just can't get it to work, always receiving a 404 error code.
I'd like to enter this url:
memorizeit.com/pics/220.0.8251.20120905002352.7982368227/Jameson+tested+the+MemorizeIt%21+Android+app%3A+With+Facebook+%26+Twitter.+Getting+grass+stains.
and have it invisibly convert to:
memorizeit.com/pics/index.php?pic=220.0.8251.20120905002352.7982368227&title=Jameson+tested+the+MemorizeIt%21+Android+app%3A+With+Facebook+%26+Twitter
The index.php page is looking for:
$pic = ($_GET["pic"]);
$title = ($_GET["title"]);
In my .htaccess file in the /pics directory I've got the following:
RewriteEngine on
Header set Cache-Control "max-age=2592000"
RewriteRule ^pics/([^\/]*)/([^\/]*)/([^\/]*)$ /pics/index.php?pic=$1&title=$2&extra=$3 [L,QSA]
RewriteRule ^pics/(.+)/(.+)$ /pics/index.php?pic=$1&title=$2 [L,QSA]
RewriteRule ^pics/([^\/]*)$ /pics/index.php?pic=$1 [L,QSA]
I've tried it without the QSA, I've tried it with either .+ (anything) and [^/]* (anything except /) I left both in so you can see how I've put them there (I think!). I do plan on making the $1 allowed to only include numbers and periods, but I'd just like to get it to work being wide open first.
I can't figure out why it isn't working. In my base url .htaccess file I have:
RewriteCond %{SERVER_PORT} ^80$
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
And it always redirects to https so I know the .htaccess files are being read. I don't know what else to try. Any thoughts would be greatly appreciated. Thanks!
Turns out that because pics/ is a real directory the rewrite was trying harder to get there than to just go ahead and rewrite it. So I changed the pics/ to p/ in the matching statement and moved the rules to the base .htaccess file and it works as expected.
Wow that took a long painful time... Hope it saves someone else some time.

Redirect an URL with all variables to another URL

I need to redirect a URL to another URL. So far I have done this in the .htaccess:
Redirect 301 http://domain.com/mypage.php http://newdomain.com/mynewpage
This works fine, but it copies the GET variables in the new URL as well. So I end up with
http://domain.com/mypage.php?item=32
being redirected to:
http://newdomain.com/mynewpage?item=32
I want remove all the variables in the new ULR. I know that this has to be done somehow with mod_rewrite, but I am struggling a lot with it and can not make it work.
Any help is appreciated.
Thanks
Ok,I got it myself... here is what I have done:
RewriteEngine On
RewriteCond %{QUERY_STRING} (.*)
RewriteRule (.*) %{REQUEST_URI}?

Resources