RewriteRule in htaccess having no affect - regex

Sorry, this is probably a simple issue but I've read tons of tutorials and can't solve the issue. Here's the URL sample:
http://localhost:8106/privacy-policy/?lang=fr&dest=app
The .htaccess contents:
RewriteEngine on
RewriteRule ^privacy-policy/?lang=([a-z][a-z])&dest=app$ privacy-policy/$1 [NC,L]
When I visit the URL I don't get redirected. Any ideas?
Thanks

You can't match against the query string in a rule, you need to use the %{QUERY_STRING} variable:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^lang=([a-z][a-z])&dest=app$
RewriteRule ^privacy-policy/$ privacy-policy/%1 [NC,L]
Note that the backreference needs to be %1. If you need it to redirect the browser, you'll also need a R flag in the square brackets.

Thanks Jon Lin for the leads on the query_string and %1 edit. It didn't end up working with a copy/paste of that code but this is finally what I ended up with and it is working:
RewriteCond %{REQUEST_URI} privacy-policy
RewriteCond %{QUERY_STRING} lang=(\w+)&dest=app
RewriteRule ^privacy-policy/$ /privacy-policy/%1? [R=301,L]
Thanks again for your help on this.

Related

Replace .php in URL with / using .htaccess

I've been searching all day for a solution to replace .php in the URL with a / using .htaccess. Most of the solutions didn't work for me at all (didn't rewrite the URL, even just to remove .php) until I found this beautiful solution on SO.
https://stackoverflow.com/a/11084526/1724376
Now my issue is that it only removes the .php but does not replace it with a "/". I've tried many things with no luck but I don't know much about htaccess and rewrite conditions, etc. I'm really hoping someone here can help me.
Just so I don't get down-voted for not having tried anything, here's one that I tried but it didn't rewrite the URL at all.
RewriteCond /%{REQUEST_FILENAME}.php -f
RewriteRule ^([a-zA-Z0-9_-\s]+)/$ /$1.php
Help will be truly appreciated.
EDIT: To clarify, I want www.mysite.com/contact.php to show up as www.mysite.com/contact/
Have your rule like this:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1/ [R=302,L,NE]
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]

htaccess Rewrite Rules appending parameter?

Our original urls began with a parameter and the following rewrite works to redirect them (thanks to Jon Lin).
However, the original parameter is being appended to redirect, so that
RewriteEngine On
RewriteCond %{QUERY_STRING} ^old-page$
RewriteRule ^$ /newpage [R=301,L]
ends up going to mydomain.com/newpage?old-page
Any idea how to fix? thanks again,
Geoff
Have it like this to strip off existing query string:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^old-page$ [NC]
RewriteRule ^$ /newpage? [R=301,L]
Take note of ? at the end of target URI, that is used to strip-off existing query string.

String start with and end with anything (but is not .php)

Trying to make a redirection URL in htaccess.
I want to redirect URLs like
www.domain.com/pageANYTHING
to
www.domain.com
But I have an exception : when I got this
www.domain.com/page.phpANYTHING
do nothing.
Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^\.phpANYTHING [NC]
RewriteRule ANYTHING$ http://www.domain.com/? [L,R,NC]
Try this:
RewriteEngine On
RewriteRule ^page(?!\.php).*$ / [R=301,L]
In fact It works with a mix of your two propositions.
RewriteCond %{REQUEST_URI} !\.(?:php)$
RewriteRule ^page(?!\.php).*$ / [R=301,L]
Thank you so much guys.

HTACCESS PUZZLE! Why does "?" work in one Rewrite statement and doesn't in another? See example

So, I've been working on htaccess since like yesterday. I have two Rewrite statements in my htaccess file.
RewriteRule ^.*wp-login\.php\?loggedout=true.*$ /not_found [R,L]
The above statement works. While
RewriteRule ^.*wp-login\.php\?action=login.*% /not_found [R,L]
...doesn't!
To make the second case work, I used the following statements.
RewriteCond %{QUERY_STRING} action=logout
RewriteRule ^wp-login\.php$ /not_found/? [R,L]
So, not using "?action=logout" in the RewriteRule in the second case seems to solve the problem.
Yes, the problem is solved, but I'd like to understand why. This is so puzzling.
Any help is appreciated. Thank you.
Your earlier rules are incorrect because QUERY_STRING cannot be matched in RewriteRule. RewriteRule only match request uri without query string.
So correct rules are:
RewriteCond %{QUERY_STRING} (^|&)action=logout(&|$)
RewriteRule ^wp-login\.php$ /not_found/? [R,L]
OR this to include both query parameters:
RewriteCond %{QUERY_STRING} (^|&)action=(login|logout)(&|$)
RewriteRule ^wp-login\.php$ /not_found/? [R,L]

.htaccess Pretty URL not displaying correctly with redirection

I am trying to configure a dynamic mod rewrite rule in my .htaccess file using mod_rewrite. I am very close to figuring this out. I am trying to get URLs like these:
http://www.mysite.com/index.php?service=14&title=events
http://www.mysite.com/index.php?service=48&title=planning
To automatically be rewritten to these:
http://www.mysite.com/service/14/events
http://www.mysite.com/service/48/planning
Here is my codes so far:
RewriteRule ^service/(.*)/(.*)$ index.php?service=$1&title=$2 [NC,L]
RewriteCond %{QUERY_STRING} ^service=$1&title=$2 [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index.php [NC]
RewriteRule ^index.php$ /service/$1/$2 [L,R=301]
I think there is something wrong with the last line maybe? I'm not the best at regular expressions so any help would be greatly appreciated.
Edit: Just wanted to be clear that the pretty URLs do work. However, the old URLs aren't redirecting and are still displaying in the browser.
I am not sure if this would be any easier but if I could get the URLs to look like this:
http://www.mysite.com/service/14/title/events
http://www.mysite.com/service/48/title/planning
Then that would work too. I don't really need the second query title to be in the URL but if it's easier to leave it in there, then no big deal.
Edit: Answered
Many thanks to all who helped contribute to the solution. I got this for my rewrite rule:
RewriteRule ^index/service/(.*)/(.*)/$ index.php?service=$1&title=$2
Once I added 'index' it worked with both query strings. As far as the redirect goes, I edited my php script and did all the URL redirecting there, which was a lot easier. Special thanks to mkjasinski for pointing that out.
Try this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^service/([0-9]+?)?/([a-zA-Z\-]+?)$ index.php?service=$1&title=$2 [L,NC]
and if I have run: http://localhost/service/12/This-is-text in $_GET in index.php:
array (size=2)
'service' => string '12' (length=2)
'title' => string 'This-is-text' (length=12)
you can't use special holders ($1,$2...) from previous rewrite rules into rewritecond.
change your code to:
RewriteRule ^service/(.*)/(.*)$ index.php?service=$1&title=$2 [NC,L]
RewriteCond %{QUERY_STRING} (?:^|&)service=([0-9]+)(&|$) [NC]
RewriteCond %{QUERY_STRING} (?:^|&)title=([a-z]+)(&|$) [NC]
RewriteRule ^index.php$ /service/%1/%3? [L,R=301]