I want to rewrite post.php?id=1&title=test to post/1/test
I have this code:
RewriteRule ^post/([^/]+)/([^/]+)/?$ post.php?id=$1&title=$2 [L,QSA]
This only works if I rename post.php links to /post in my php file. I want to rewrite the urls so that I don't need to edit any links.
Similar to below:
RewriteCond %{THE_REQUEST} \s/+(post)\.php[?/\s] [NC]
RewriteRule ^ %1 [R=301,L]
RewriteRule ^(post)/?$ $1.php [L]
You can use:
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+post\.php\?id=([^&]+)&title=([^\s&]+) [NC]
RewriteRule ^ /post/%1/%2? [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^post/([^/]+)/([^/]+)/?$ post.php?id=$1&title=$2 [L,QSA,NC]
But it is wrong to say "I don't need to edit any links". Because it is a method to correct the old links already referenced, not to prevent you from changing your code in your pages. Because HTML code still contains bad links...
Related
I need advise on to 301 permanent redirect using htaccess all pages having ?page=9 with index.php
Can anyone guide on how to achieve desired
For example -1
http://www.example.com/?page=9&option=com_news&view=list&Itemid=100&limitstart=840
to
http://www.example.com/index.php&option=com_news&view=list&Itemid=100&limitstart=840
or say for example 2
http://www.example.com/?page=17&option=com_news&view=list&Itemid=100
to
http://www.example.com/index.php&option=com_news&view=list&Itemid=100
Edit
Used
RewriteEngine on
RewriteCond %{THE_REQUEST} \?page=.+&(.+)\sHTTP [NC]
RewriteRule ^ /index.php&%1? [L,R=301]
But it dint worked as output is
http://www.example.com/index.php&limitstart=840
Assuming you actually want /index.php?option=... rather than /index.php&option=... - just matching on the querystring:
RewriteEngine On
RewriteCond %{QUERY_STRING} page=[^&]+&(.*) [NC]
RewriteRule .* /index.php?%1 [L,R,QSD]
However if you really do want /index.php&option=... replace the RewriteRule with:
RewriteRule .* /index.php&%1 [L,R,QSD]
Note: either way, this may not work as expected if page is not the first paramter
If your Apache version is older than 2.4 replace the RewriteRule with:
RewriteRule .* /index.php?%1? [L,R]
The [QSD] flag was only introduced with 2.4 - shouldn't be necessary anyway since the entire querystring is being matched.
You can use this :
RewriteEngine on
RewriteCond %{THE_REQUEST} \?page=.+&(.+)\sHTTP [NC]
RewriteRule ^ /index.php&%1? [L,R=301]
I have url coming on my web page like:
www.abc.com/product_list.php?id=12&pId=1&gpId=0
I want it to show like:
www.abc.com/product_list.php/12/1/0
i know this is very basic question but i am new in php.
your help will be really appreciated.
I did experiment like this but its not working: (in my htaccess file by googling)
RewriteRule ^product_list.php /product_list.php/id=$1/pId=$2/gpId=$3 [NC]
My .htaccessfile:
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteCond %{HTTP_USER_AGENT} (google|yahoo|msn|aol|bing) [OR]
RewriteCond %{HTTP_REFERER} (google|yahoo|msn|aol|bing)
RewriteRule ^([^/]*)/$ hamper-anastassia.php?$1 [L]
RewriteRule index.html$ /index.php
RewriteRule about.html$ /about.php
RewriteRule products.html$ /products.php
RewriteRule partners.html$ /partners.php
RewriteRule career.html$ /career.php
RewriteRule contact.html$ /contact.php
RewriteRule products_(.*)_(.*).html$ products.php?id=$2 [L]
RewriteRule ^product_list/([0-9]+)/([0-9]+)/([0-9]+)/?$ /product_list.php?id=$1&pId=$2&gpId=$3 [NC]
You should remove the .php
A simple example to capture the first parameter.
RewriteRule ^products/([0-9]+)/?$ show_a_product.php?product_id=$1
More here
https://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/
Basically every pairing of brackets catches a variable, this is where the $1, $2, $3 comes from.
RewriteRule ^product_list.php/([0-9]+)/([0-9]+)/([0-9]+)/?$ /product_list.php?id=$1&pId=$2&gpId=$3 [NC]
As Geoffrey mentions you could go one step further and remove the need for .php but that's up to you, e.g:
RewriteRule ^product_list/([0-9]+)/([0-9]+)/([0-9]+)/?$ /product_list.php?id=$1&pId=$2&gpId=$3 [NC]
Meaning you could visit /product_list/12/1/0
Please remove/comment everything in your .htaccess file then type only these two statements
RewriteEngine on
RewriteRule ^products/([0-9]+)/([0-9]+)/([0-9]+)/?$ products.php?id=$1&pid=$2&gpid=$3
Your url should be like this:
/products/1/2/3
If your still getting and error check your mod_rewrite configurations
I am using rewriting in my .htaccess like this:
RewriteRule ^blog$ blog.php [L]
It works, that's cool (when I put www.something.com/blog, it get blog.php content), but I would love to avoid possible duplicates of my web.
So I want to redirect www.something.com/blog.php only to www.something.com/blog.
Add this new rule on top:
RewriteCond %{THE_REQUEST} \s/+(blog)\.php[\s?] [NC]
RewriteRule ^ /%1 [L,R]
Currently I set a rewrite rule like so, to produce clean and simple url's.
.htaccess
RewriteRule ^/about$ about.php [L]
But what i need to do is something a little different, the other way around. For example if a user clicks the following link
about
They would go to the about page /about
Currently the about page resides at index.php?a=about&b=user and this can't be changed unfortunately. As you can see it does not look very nice in the browser address bar.
EDITED
I am using a pre-made script from phpdolphin, which is working fine. But the url are all index.php based and i would like to clean them up
Currently the only code within the .htaccess file
RewriteEngine on
RewriteCond %{request_filename} -f
RewriteRule ^(.*) $1 [L]
RewriteRule ^(([^/]*)+)(/([^/]{0,32})(/.+)?)?$ index.php?a=$1&q=$3 [L]
Add this rule before your existing rule:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?a=([^&]+)&b=user[\s&] [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteRule ^([^/.]+)/?$ index.php?a=$1&b=user [L,NC,QSA]
You can add this RewriteRule to redirect the request when user hit index.php?a=about&b=user
RewriteCond %{QUERY_STRING} ^(.*)a=about(.*)$ [NC]
RewriteRule ^index.php /about [L,NC,R=301]
or you can use php header() function in index.php to redirect the request:
if ($_REQUEST['a'] == about) {
header('Location: /about');
exit;
}
Try this i checked and it works ...
RewriteEngine On
RewriteRule ^([A-Za-z0-9-+_%*?]+)/([A-Za-z0-9-+_%*?]+)/?$ index.php?a=$1&q=$2 [L]
Note: add additional signs between [] if necessary
OR This
RewriteRule ^([^~]+)/([^~]+)/?$ index.php?a=$1&q=$2
I like to use this code because it's short and matches everything except for ~ which is very very rare to see in a url
I'm trying to add some rewrite rules to my htaccess file and I'm having a bit of trouble with a few bits.
I've currently got the following set up (bits are from different sites so I'm sorry if it's not consistent!):
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /search\.php\?part=([^&]+)\ HTTP/
RewriteRule ^search\.php$ http://test.dev/part/%1? [R=301,L]
RewriteRule ^part/(.+)$ /search.php?part=$1 [L]
This allows me to go to test.dev/search.php?part=foo and it rewrites/redirects the url to test.dev/part/foo - this works fine and shows me the correct content.
However, I can also go to test.dev/search.php?part=foo,bar and it rewrites/redirects the url to test.dev/part/foo,bar - even though this shows up the correct content, this is not what I want the url to look like.
Is there a way to take into account what's in the url after 'part' and if it finds a comma, remove the comma and anything after it?
I'd still need the 'foo' and 'bar' to be read by my code and show the correct content for those two options but for the url to show test.dev/part/foo (as foo was the first one in the url).
Replace your with this:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\s/+search\.php\?part=([^&,\s]+) [NC]
RewriteRule ^ /part/%1? [R=301,L]
RewriteRule ^part/(.+)$ /search.php?part=$1 [L,NC]
UPDATE: To capture string after comma into query string
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\s/+search\.php\?part=([^&,\s]+)(?:,([^&,\s]+))? [NC]
RewriteRule ^ /part/%1?q=%2 [R=301,L]
RewriteRule ^part/(.+)$ /search.php?part=$1 [L,NC,QSA]