An external app calls my website with the following URL:
http://www.mydomain.com/index.php?route=checkout/success?ref=56
which is obviously wrong (since it has 2 questionmarks).
How can I write an .htaccess rewrite rule to convert the second questionmark to ambersand (&) ? (http://www.mydomain.com/index.php?route=checkout/success&ref=56)
I have tried this but it does not work.
RewriteEngine on
RewriteRule \? \& [L]
Well after some research, I have come up with this
RewriteCond %{QUERY_STRING} ^(.*)?\?ref=(.*)&?(.*)$
RewriteRule (.*) /$1?%1&ref=%2%3 [L]
I do not know if it's the best but it works for sure
RewriteEngine on
RewriteRule /(.*)?(.*)?ref=(.*)$ /$1?$2&ref=$3 [R]
RewriteLogLevel 9
RewriteLog "/var/log/rewrite.log"
must admit I have not seen it work on my end but it should be nearly there - enabled rewrite log so it can be debugged easier
Related
I need to do some redirecting to get some internal links to work but I'm having a complete block.
The url would be http://www.something.com/faqs/What_happens_if_I_move_home?
redirected to http://www.something.com/faqs/index/What_happens_if_I_move_home?
but it must look like the original url. I'm sure there is a simple answer but rewrite rules and regex are a mystery to me at times.
I did try RewriteRule ^faqs(/.*)?$ /faqs/index$1 [R,L,NC]
amongst many others!
try this:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/faqs/index
RewriteRule ^faqs/(.*) /faqs/index/$1 [L,NC]
I want to automatically redirect http requests to news/images to ../images.
Is that possible with .htaccess?
Thing is: request to www.site.tld/news/images ... should go to www.site.tld/images ...
I have tried:
RewriteEngine On
...
...
RewriteRule (.*)news/images(.*) ../images [R=301,L]
not working.
I have ensured that apache have mod_rewrite.c enabled.
To redirect all requests for /news/images/ to /images/, capture the part after images and use it in the RewriteRule
RewriteRule ^news/images(.*)$ /images$1 [R,L]
When it works as it should, you may replace R with R=301. Never test with R=301.
You can use:
RewriteRule ^www\.site\.tld/news/images$ /www.site.tld/images?&%{QUERY_STRING}
or you can also use:
RewriteCond %{HTTP_HOST} ^www.site.tld/news/images$ [NC]
RewriteRule ^(.*)$ http://www.site.tld/images/$1 [R=301,L]
But as #arkascha said, please do some research first, there are MANY answers to this sort of problem! :) Either way, I hope this helps.
I am fiddling around with .htaccess and mod_rewrite. I have a site that has two types of URLs which I want to rewrite:
/index.php?nav=$2
/index.php?nav=41&intNewsId=$3 -- 41 is static, the news nav is always 41
I want to rewrite them to:
/pagename/id
/news/pagename/id
I already made a piece of code that works, BUT if I add the last line the second line stops working, and I can imagine thats because the conditions in the third block are also true for the second block. But I cant figure out how to use conditions right. (Both blocks work individual)
Options +FollowSymlinks
RewriteEngine on
# Reroute rules that end on /
RewriteRule ^(.*)\/([0-9]|[1-9][0-9]|[1-9][0-9][0-9])$ /$1/$2/ [R]
# Make the system understand pagename/96
RewriteRule ^(.*)\/([0-9]|[1-9][0-9]|[1-9][0-9][0-9])/$ /index.php?nav=$2
# Make the system understand news/pagename/99
RewriteRule ^(.*)\/(.*)\/([0-9]|[1-9][0-9]|[1-9][0-9][0-9])/$ /index.php?nav=41&intNewsId=$3
I tried everything I could think of, but I'm not too familiar with this regex style of typing or conditional blocks in htaccess.
Solution:
I fixed my own code, I just stripped the second $ so the condition didnt interfere with the last one
Options +FollowSymlinks
RewriteEngine on
# Reroute rules that end on /
RewriteRule ^(.*)\/([0-9]|[1-9][0-9]|[1-9][0-9][0-9])$ /$1/$2/ [R]
# Make the system understand pagename/96
RewriteRule ^(.*)\/([0-9]|[1-9][0-9]|[1-9][0-9][0-9])/ /index.php?nav=$2
# Make the system understand news/pagename/99
RewriteRule ^(.*)\/(.*)\/([0-9]|[1-9][0-9]|[1-9][0-9][0-9])/$ /index.php?nav=41&intNewsId=$3
Thanks for the answers all!
Try this:
RewriteRule ^news/.+/([^/]*)$ /index.php?nav=41&intNewsId=$1 [L]
RewriteRule ^.+/([^/]*)$ /index.php?nav=$1 [L]
Another approach, perhaps slightly more concise:
RewriteRule ^pagename/(\d+)$ index.php?nav=$1
RewriteRule ^news/pagename/(\d+)$ index.php?nav=41&intNewsId=$1
Try this:
# Make the system understand pagename/96
RewriteCond %{REQUEST_URI} ^/pagename/([0-9]*)
RewriteRule .* /index.php?nav=%1
# Make the system understand news/pagename/99
RewriteCond %{REQUEST_URI} ^/news/pagename/([0-9]*)
RewriteRule .* /index.php?nav=41&intNewsId=%1
Currently my .htaccess looks like this...
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,QSA]
It currently changes any /xxx.php file into /xxx. This is great for SEO. However, I also want Mr. htaccess to convert certain URLs into a URL + query string. For instance when user goes to
/specific/somerandominfo
Then somerandominfo is passed to the specific.php file. I normally have no problem doing this using rewrites, but because of my fancy catchall rewrite, I can't figure out how to do it.
For example if I add
RewriteRule ^specific/([^/]+)$ /specific.php?somerandominfo=$1 [NC]
to my .htaccess, then hitting up /specific/somerandominfo just serves me a big fat 500 Internal Service Error.
Any help from you apache gurus out there would be so, so cool.
Thanks!
p.s. anybody want to also throw in any other cool SEO tricks that they like? I'll bake you cookies.
You are getting 500 error because your rules are creating an infinite cycle. Check apache error log to see if it is true. So you should design your rules properly. Maybe like that:
RewriteRule ^([^/]*)$ $1.php [L]
RewriteRule ^(.*)/(.*)$ $1.php?var=$2 [L]
RewriteRule ^specific/([^/]+)$ /specific.php?somerandominfo=$1 [NC]
This is mostly correct. I'd just add the B flag, like this:
RewriteRule ^specific/([^/]+)$ /specific.php?somerandominfo=$1 [NC,B]
This causes the capture group $1 to be properly escaped for use in query strings. Note that you can still use QSA to retain the query parameters used in the original request (in addition to somerandominfo).
Perhaps you'll want to post your actual RewriteRule.
I have a script that echoes a meta redirect to a page called account_management.php5, but for some reason it automatically redirects from there to index.php5. My .htaccess file handles a couple of redirects automatically, for example index.html|php5 to the domain root, and that's the only place I can see this problem originating, but I don't understand why. This is my .htaccess file:
RewriteEngine On
#remember to change this to aromaclear
RewriteCond %{HTTP_HOST} !^sinaesthesia\.co.uk$ [NC]
RewriteRule ^(.*)$ http://sinaesthesia.co.uk/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^GET\ .*/index\.(php5|html)\ HTTP
RewriteRule ^(.*)index\.(php5|html)$ /$1 [R=301,L]
#translate any .html ending into .php5
RewriteRule ^(.*)\.html$ /$1\.php5
#change / for ?
RewriteRule ^(.*)\.html/(.*)$ /$1\.html?$2
#strip .html from search res page
RewriteRule ^(.*)search/(.*)$ /$1search_results\.html/search=$2
#translate product details link from search res page
RewriteRule ^products/(.*)/(.*)/(.*)$ /product_details.php5?category=$1&title=$2&id=$3 [L]
#Translate products/psorisis/chamomile-skin-cream-P[x] to productview.php5?id=1
RewriteRule ^products/.*-P([0-9]+) /productview.php5?id=$1 [L]
Wrong:
RewriteRule ^(.*)\.html$ /$1\.php5
Right:
RewriteRule ^(.*)\.html$ /$1.php5
Righter:
RewriteRule ^(.*)\.html$ /$1.php5 [QSA]
This same mistake of escaping special chars in the second param of RewriteRule is happening in other rules too, I don't know if apache will handle it, but I know you don't need it because second param is not a regexp.
Never compare to %{THE_REQUEST}, thats a weird thing to do, you don't need that. Moreover, this condition is fine without it. Just put there:
RewriteRule ^(.*)index\.(php5|html)$ $1 [R=301,QSA,L]
Now look at it:
RewriteRule ^(.*)\.html/(.*)$ /$1.html?$2
First, you are still accepting that there are references to .html files, just after trying to translate all .html to .php5, there's something wrong here.
Moreover, you are defineing as QueryString something that was originally a file path, and are not even putting it in a key. It won't work, it need some more treatment.
#strip .html from search res page
RewriteRule ^(.*)search/(.*)$ /$1search_results.html/search=$2
Wasn't it supposed to strip the .html? Because it is actually putting a .html there. Maybe as it is not an [L] it get fixed in the next loop, but you could just get all fixed right here.
#translate product details link from search res page
RewriteRule ^products/(.*)/(.*)/(.*)$ /product_details.php5?category=$1&title=$2&id=$3 [L]
This one full of .* is potentially unstable, specially delimitating the end. You should do this:
RewriteRule ^products/([^/]*)/([^/]*)/([^/]*) /product_details.php5?category=$1&title=$2&id=$3 [L]
# or:
RewriteRule ^products/(.*?)/(.*?)/([^/]*) /product_details.php5?category=$1&title=$2&id=$3 [L]
The last one looks correct, except that you should strip the special character that may be faced as a range delimiter, the "-". I don't think it work after a *, but just to be sure and correct the syntax:
RewriteRule ^products/.*\-P([0-9]+) /productview.php5?id=$1 [L]
Add this just after RewriteEngine on
RewriteLogLevel 9
RewriteLog /tmp/rw.log
Then restart the webserver. It should help you debug the problem.
Edit: Sorry, I didn't notice the .htaccess above. This will only work from the main apache configuration file.