I am trying to get Apache to redirect /a.php?a=123 to /b/123 (where 123 could be any number between 1 and 9999) but can't seem to get it to work.
This is what I have in htaccess:
RewriteEngine on
RewriteRule ^a.php?a=([0-9]+) /b/$1 [L]
RewriteRule ^a.php$ /c/ [L]
With this going to a.php?a=123 results in 404, but going to just a.php works as expected.
I tried escaping the ? (RewriteRule ^a.php\?a=([0-9]+) /b/$1 [L]) but it still doesn't work.
What am I doing wrong please?
The query string is not part of the URI path that is tested in the RewriteRule directive. This can only be tested with a RewriteCond directive:
RewriteCond %{QUERY_STRING} ^a=([0-9]+)$
RewriteRule ^a\.php$ /b/%1? [L,R]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^a\.php$ /c/ [L,R]
But if you want it the other way (requests of /b/123 are redirected to /a.php?a=123):
RewriteRule ^b/([0-9]+)$ a.php?a=$1 [L]
Related
I have rewrite:
RewriteRule (.*\.html$) /index.php?$1 [L,QSA]
but some pages have pagination, for example
/test.html?page=2
i need to rewrite it to test-page2.html
i tried
RewriteRule (.*)-page([0-9]*)\.html$ /index.php?$1&page=$2 [NC,L]
but it is not working
With your shown samples, please try following. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##new rule added by me. This will check if a url has html as well as query string then it will rewrite it to html file.
RewriteCond %{THE_REQUEST} \s/([^.]*)\.html\?(page)=(\d+)\s [NC]
RewriteRule ^ %1-%2%3.html [R=301,L]
##OP's previous rule.
##For safer side you could add an additional condition here too.
RewriteCond %{QUERY_STRING} ^$
RewriteRule (.*\.html)$ /index.php?$1 [L,QSA]
Does this work for you?
RewriteEngine on
RewriteCond %{QUERY_STRING} ^([^&]+)&page=([0-9]+)$ [NC]
RewriteRule ^index\.php$ /%1-page%2.html? [L,R]
RewriteRule ^([^-]+)-page([0-9]+)\.html$ /index.php?$1&page=$2 [END]
Make sure to clear your browser cache or use different browser to test this code.
I would like to redirect, for example :
index.php?name=sarah
to
names/sarah.php
The folder structure:
names/...
index.php
I've tried :
RewriteRule index.php?name=$1 /names/$1.php
and other ones, but didn't came to a solution.
Is this possible? If so, how can you do this?
Note:
The other way round is
RewriteRule ^names/([^/]*)\.php$ index.php?q=$1 [QSA,L]
which works fine.
Here is your rule:
RewriteCond %{QUERY_STRING} name=([^&]*)
RewriteRule ^.*$ /names/%1.php?
You first example wont work as you are trying to manipulate query strings using a RewriteRule but this is not allowed. You can try the following :
RewriteEngine on
RewriteCond %{THE_REQUEST} / index\.php\?q=([^\s]+) [NC]
RewriteRule ^ /names/%1.php? [L,R]
RewriteRule ^names/([^/]*)\.php$ index.php?q=$1 [QSA,L]
Here's my code in htaccess:
RewriteCond %{THE_REQUEST} \s/user/\?user=(.*)\s [NC]
RewriteRule ^ /user/%1? [R=301,L]
RewriteRule ^user/(.*)$ /user/?user=$1 [L]
What I want it to do is take the URL /user/?user=username and rewrite it to /user/username
What I have successfully rewrites it to /user/username but then it gives me a 500 error. If anyone could tell me why I would be very appreciative.
Thanks!
Edit:
The 500 error seems to be because it's creating a redirect loop with this rule. I'm not sure how to take just the last part and append it to the URL as a query string.
Please change this line :
RewriteRule ^user/(.*)$ /user/?user=$1 [L]
with this :
RewriteRule ^user/[a-zA-Z]+$ /user/?user=$1 [L]
You should match only letters , otherwise the above condition RewriteCond %{THE_REQUEST} \s/user/\?user=(.*)\s [NC] doesn't make any sense with (.*)$
This may be a basic question regarding RewriteRule but I just counldn't make it work.
What I want to do is detect urls like:
mydomain/myapp/id/some/random/url.html?param=somethingThatIdoUse
mydomain/myapp/id/other/randomabc/perrito.php?param=somethingThatIdoUse
...
The part that I need to discart is from the /id/[all this]?param=somethingIdoUse and use the param if is possible or send the complete url as param so I can regex to get the param.
And have a rule that detect that /id/ exist and redirect to something like:
mydomain/myapp/other/manageRequest.php?params=somethingThatIdoUse
(the params I could get the whole url and strip it as string is no problem)
As well the application have different modules like:
mydomain/myapp/moduleOne/index.php
mydomain/myapp/moduleTwo/index.php
This have to keep working the same.
As far I've tried some of them like:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET /.*;.* HTTP/
RewriteCond %{QUERY_STRING} !^$
RewriteRule .* http://localhostdev/app/index.php %{REQUEST_URI}? [R=301,L]
RewriteEngine On
RewriteBase /spt/
RewriteCond %{REQUEST_FILENAME} !^id$ [NC]
RewriteRule ^(.*)$ index.php [R=301,L]
RewriteEngine On
RewriteCond %{REQUEST_URI} !/campaings/response\.php$
RewriteRule ^/something/(.*) /other/manageRequest.php? [L]
But nothing seamed to do kind of what I needed.
Thanks in advice
mydomain/myapp/id/some/random/url.html?param=somethingThatIdoUse
Here is an example using the above URL:
RewriteRule ^id/some/random/url.html/? /myapp/other/manageRequest.php [L,NC]
The query will be passed through unchanged to the substitution URL
well actually end up being really basic it worked with:
RewriteRule ^.*id.*$ handleRequest.php [NC,L]
I do get the params as they are sent!
i would like to shorten
www.site.com/product/info/laptop to www.site.com/laptop
I used
RewriteRule ^(.+)$ /product/info/$1
but i get 500 Internal Server Error
when i try,
RewriteRule ^([a-zA-Z0-9_\s\'~%,:!?()_=&-]+)$ /product/info/$1
it works but i want to support the period as well, so when I include .
RewriteRule ^([a-zA-Z0-9_\s\'~%,:!?()\._=&-]+)$ /product/info/$1
It gives me 500 Internal Server Error
Could you explain what is going on?
Thank you
Try the following:
RewriteCond %{REQUEST_URI} ^/product/info/(.*)$
RewriteRule ^(.*)?$ /%2 [R=301,L]
That will redirect the browser from www.example.com/product/info/laptop to www.example.com/laptop with a "Moved Permanently" header.
If you mean you wish the shorter URL to point to the longer URL internally, then you must avoid circular redirections:
RewriteRule ^product/info/.*$ - [L] # don't redirect again
RewriteRule ^(.*)$ /product/info/$1 [L] # redirect everything else
The first line will stop trying to redirect www.example.com/product/info/laptop to www.example.com/product/info/product/info/laptop, and so-on.
Edit
Based on your comment, it looks like you're also trying to redirect everything except img, phpmyadmin, etc, to index?whatever - You have to rearrange it all a bit now, something like this:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/(index\.php|img|phpmyadmin|images|user|wmd|FCKeditor|map|jscalendar|aurigma|xajax_js|css|js|include|floatbox|helper|styles|ajax|robots\.txt|favicon\.ico|product/info/(.*))
RewriteRule ^(.*)$ - [L] # don't redirect these
RewriteRule ^(.*)$ /product/info/$1 # redirect everything else
I'm not 100% on the "product/info/(.*)" part of the first rewrite. If that doesn't work, try this:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/(index\.php|img|phpmyadmin|images|user|wmd|FCKeditor|map|jscalendar|aurigma|xajax_js|css|js|include|floatbox|helper|styles|ajax|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ - [L] # don't redirect these
RewriteRule ^product/info/.*$ - [L] # don't redirect again
RewriteRule ^(.*)$ /product/info/$1 [L] # redirect everything else
Edit 2
Final answer based on your comment:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/(index\.php|img|phpmyadmin|images|user|wmd|FCKeditor|map|jscalendar|aurigma|xajax_js|css|js|include|floatbox|helper|styles|ajax|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ - [L] # don't redirect these
# pass controllers to index.php
RewriteCond %{REQUEST_URI} ^/(home|browse|calendar|star|member|enter|product)
RewriteRule ^(.*)$ /index.php?$1 [L]
# pass other things than controllers to /index.php?/product/info/$1
RewriteRule ^(.*)$ /index.php?/product/info/$1 [L] # redirect everything else