Should be a simple mod_rewrite rule - php

This rule does not seem to be functioning, however I cannot determine why, it looks fine to me:
RewriteRule ^shop_en/proddetail.php?prod=(.+)$ /shop/product/$1 [L,NC,NE,R=301]
Its like it does not get processed at all, I simply get my sites error message stating it cannot find 'proddetail.php'.
Which is weird too, because on a quick test I tried the following:
RewriteRule ^shop_en/proddetail.php$ /shop/product [L,NC,R=301]
Which worked fine. So what is going on?
Edit: The product ID's that I am working with can contain letters, numbers and dashes.
In the simplest terms, with the most convenient definitions:
I want to route anyone trying access:
www.domain.com/shop_en/proddetail.php?prod=333
To the following URL:
www.domain.com/shop/product/333
how can I do this?

Try this
RewriteRule ^/?shop/product/([a-zA-Z0-9\-]+)$ shop_en/proddetail.php?prod=$1 [L,NC,NE,R=301]

It is not working because you're trying to match QUERY_STRING in RewriteRule. You will need a RewriteCond to match QUERY_STRING.
You can use:
RewriteCond %{THE_REQUEST} \s/+shop_en/proddetail\.php\?prod=([^&\s]+) [NC]
RewriteRule ^ /shop/product/%1? [L,NE,R=301]
RewriteRule ^shop/product/([^/]+)$ /shop_en/proddetail\.php\?prod=$1 [L,QSA,NC]

Related

multiple rewrite for a single url using htaccess

I have a multi blog website in which there are multiple domains.
The thing is i need a rewrite rule for the following syntax
XXX.domain.com/{any other things} to www.domain.com/domain/XXX/{any other things}
Also i had written a set of rules for the below one too
^domain/([a-zA-Z0-9]+)/cat/([a-zA-Z0-9]+)$ index.php?domain=$1&cat=$2
So the things is i need to construct a rule which converts domain to path then again process the other things as follows with that.
Before your other rule, you'll need
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^.]+)\.
RewriteCond $1 !^domain/
RewriteRule ^(.*)$ /domain/%1/$1 [L]
RewriteRule ^([a-zA-Z0-9\-_]+).domain.com/(.*)$ www.domain.com/$1/$2
([a-zA-Z0-9\-_]+) matches a subdomain.
(.*) matches everything.
This will rewrite whatever.domain.com/anything to www.domain.com/whatever/anything.
I hope I understood correctly, that your redirect example has nothing to do with the requirement:
RewriteRule ^([^.]+)\.domain\.com/(.*)$ www.domain.com/domain/$1/$2
[^.]+ captures the subdomain, the rest is selfevident, I hope.

Rewriting The URL With .htaccess

Ok, So I've looked at this topic for quite a while now and can't get anything to work, probably because I'm still having difficulty understanding it - So I'm going back to basics and asking this in the simplest of terms.
I have an empty .htaccess file
I have a current URL of http://www.website.co.uk/news.php?id=111111
I want this to become http://www.website.co.uk/news/111111
How Do I Do This?
Also please not that although this is the URL now, I'm planning on making some changes to the site so the URL's in the future may be:
http://www.website.co.uk/news.php?city=city&issue=1&title=the-title&id=111111
http://www.website.co.uk/news/city/issue/the-title/111111
How can I make it so that the future changes will work too? So far I have:
RewriteEngine On
RewriteRule ^news/(.+)$ news.php?id=$1 [L]
This still displays the full url and typing in news/111111 redirects to an error page. Please help!
Adding the following to your htaccess should work:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule ^news.php$ /news/%1? [L,R]
RewriteRule ^news/(.*) news.php?id=$1 [QSA]
The above will change http://www.website.co.uk/news.php?id=111111 to http://www.website.co.uk/news/111111
and below will change
RewriteCond %{QUERY_STRING} ^city=(.*)&issue=(.*)&title=(.*)&id=(.*)$
RewriteRule ^news.php$ /news/%1/%2/%3/%4 [L,R]
RewriteRule ^news/(.*)/(.*)/(.*)/(.*) news.php?city=$1&issue=$2&title=$3&id=$4 [QSA]
http://www.website.co.uk/news.php?city=city&issue=1&title=the-title&id=111111 into http://www.website.co.uk/news/city/issue/the-title/111111
The values in %1, %2, $3, %4 gotten from the parameters after city=, issue=. title=. id=.
In city=London, London will be contained in %1 etc
The second RewriteRule will allow you to find the id used.

Regex - mod_rewrite - Match string after characters and run as get vars

I'm looking to find and capture the url by "/-" and run the resulting find as a single get var on the preceding page:
http://domain.com/about-us/contact/-foo/-bar
would run as:
http://domain.com/about-us/contact/?get=/-foo/-bar
I'm able to find the match with:
RewriteCond %{REQUEST_URI} (\/-.*)
RewriteRule ^(.*)$ ?get=$1 [L]
But have been unsuccessful returning the proper get var. This yields:
http://domain.com/?get=about-us/contact/-foo/-bar
Any help would be much appreciated!
You're matching the whole URL in the RewriteRule, instead break it up into the base and the rest since you already predetermined the URL format with RewriteCond:
RewriteCond %{REQUEST_URI} (\/-.*)
RewriteRule ^(.*?)(\/-.*)$ $1?get=$2 [L]
Note: There are other ways to do this without RewriteRule. However, this is most like your current code.
Ok, I found the issue to the redirect vs. rewrite I was having. Basically I need to make sure and put the file in index.php.
RewriteCond %{REQUEST_URI} (\/-.*)
RewriteRule ^(.*?)(\/-.*)$ $1/index.php?get=$2 [L]
Thanks, Jason for your help!

URL Mod Rewriting

I am using codeigniter for my website, and before theres always that index.php? on my every url or links, for example
mysite.com/index.php?/about
Google has indexed all of my urls with that index.php? and I want to remove it and redirect it without that. I am having a problem rewriting the URL and redirect it to mysite.com/about and this what i have tried so far
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?(/[^\s\?]+)? [NC]
RewriteRule ^ %1/ [QSA,L,R=301]
what happened is, it only removed the index.php,
for example mysite.com/index.php?/about will turn to mysite.com/?/about I don't know how to remove that question mark,
I'm not good on mod_rewrite thanks in advance for the help.
I think you can improve the rules slightly.
RewriteCond %{REQUEST_URI}?%{QUERY_STRING} index\.php?\?.+
RewriteRule .*$ %{QUERY_STRING}? [R=301,L]
Essentially, you don't have to worry about the entire request line in %{THE_REQUEST}, which removes all the complicated regex. Also, the rule redirects to whatever is listed in %{QUERY_STRING}, and removes the query string.
I am not sure why you used QSA in the first place. I think that was part of the problem earlier. Just for an exercise, you can try removing QSA and see what happens.
You should try this one.
RewriteCond %{QUERY_STRING} ^/[a-z]+$ [NC]
RewriteRule ^/index.php$ %{QUERY_STRING} [NC,L,R=301]

mod_rewrite with ? in the original URL like YouTube

Ok I want to simulate the YouTube URL and redirect it to my real URL. Reason being is I used a random string to make video ID's so they aren't guessable by sequence.
I have the links as so
http://www.mysite.com/watch?v=Dxdotx3iT1
and want to redirect to
http://www.mysite.com/index.php?page=videos&section=view&v=Dxdotx3iT1
Can't seem to figure out the mod rewrite. Also using a ? I believe tells it to do something.
Any help would be appreciated.
You need to adjust your RewriteRule to include the query string using the [QSA] (query string attached) flag:
RewriteRule ^watch$ index.php?page=video&section=view [QSA]
RewriteCond %{QUERY_STRING} ^v=(.+)
RewriteRule ^watch /index\.php?page=videos&section=view&v=%1 [QSA,R=301,L]
None of these answers worked for me, so in the end I found through trial and error the following worked for me;
The RewriteRule to get my URL to look like this https://www.example.com/video/watch?v=UnIq3Id follows;
RewriteRule ^video\/watch\?*$ index.php?page=video [L,QSA]
I found that the following rule I had previously set up to make my URL look like this https://www.example.com/video/UnIq3Id interfered with my redirecting;
RewriteRule ^/?([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)$ index.php?page=$1&v=$2
Simply commenting it out fixed the issue, as follows;
#RewriteRule ^/?([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)$ index.php?page=$1&v=$2
The RewriteRule to get my URL to look like this https://www.example.com/watch?v=UnIq3Id is as follows;
RewriteRule ^watch\?*$ index.php?page=video [L,QSA]
I found that the following rule I had previously set up to make my URL look like this https://www.example.com/video/ interfered with my redirecting;
RewriteRule ^([A-Za-z0-9_-]+)/?$ index.php?page=$1 [NC]
Simply commenting it out fixed the issue, as follows;
#RewriteRule ^([A-Za-z0-9_-]+)/?$ index.php?page=$1 [NC]
Hope it helped someone and saved you the headache I had, people are not so forthcoming on this issue at times How do you write an htaccess RewriteRule to make my urls work like youtube?.

Categories