I am trying to rewrite a URL so that it is SEO friendly and excludes the product ID. Currently I am using this in my .htaccess file:
RewriteRule ^shop/product/([^/\.]+)/([^/\.]+)$ products.php?id=$2&name=$1 [NC,L]
For example rewrites it to:
shop/product/product-name/12
Is there a way that I can rewrite this URL so it removes or excludes the product ID from the end?
Could you please try following, written and tested with shown samples. Please make sure you clear your browser cache before testing your URLs.
First set of Rules is considering that your product.php is present in root, in same path as .htaccess is present.
RewriteEngine ON
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^shop/product/([^/]*)/([^/]*)/?$ /products.php?id=$2&name=$1 [NE,R=301,NC]
RewriteCond %{QUERY_STRING} !^$
RewriteCond %{THE_REQUEST} \s/products\.php\?id=[^&]*&name=[^\s]*\s [NC]
RewriteRule ^(.*)$ shop/product [R=301,QSD,NE,L]
OR Please use either above OR following rules at a time only in your .htaccess.
RewriteEngine ON
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^shop/product/([^/]*)/([^/]*)/?$ products.php?id=$2&name=$1 [NE,R=301,NC]
RewriteCond %{QUERY_STRING} !^$
RewriteCond %{THE_REQUEST} products\.php\?id=[^&]*&name=[^\s]*\s [NC]
RewriteRule ^(.*)$ shop/product [R=301,QSD,NE,L]
Related
I need to make a 301 redirect from
https://www.example.net/sub1/specific_keyword/page1/page2
https://www.example.net/sub2/sub3/specific_keyword/page3/page4
https://www.example.net/specific_keyword/page5/page6
to
https://www.example.net/sub1/
https://www.example.net/sub2/sub3/
https://www.example.net/
I tried this code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.net$ [NC]
RewriteRule ^(.*)\/specific_keyword$ "https\:\/\/www\.example\.net\/$1" [R=301,L]
But no luck.
With your RewriteRule attempt, you demanded that the requested URL
ends with /folder-to-remove, via the $ at the end, so that won’t match (Source: #comment120998408_68464303)
With that fixed, place following rules at top of your htaccess Rules file. Make sure to clear your browser cache before testing your URLs or use a redirect checker online.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^www\.example\.net$ [NC]
RewriteRule ^([^/]*)/([^/]*)/.*$ $1/$2? [R=301,NE,L]
OR only match the specific keyword
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)specific_keyword/ https://www.example.com/$1 [R=301,L,NE]
I want to redirect pages like:
/category-name/post-name.html?id=1234
To:
/category-name/1234-post-name.html
How can do this using htaccess?
What I have tried:
RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule ^([^/]+)/([^.]+)\.html$ /$1/%1-$2\.html [L,R=301]
But it is a continuous redirect.
You can use these rules in your site root .htaccess:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+([\w-]+)/([\w-]+)\?id=([^\s&]+) [NC]
RewriteRule ^ /%1/%3-%2? [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^([\w-]+)/([^-]+)-([\w-]+)/?$ $1/$3?id=$2 [L,QSA]
Try this
Make sure the url is root url
Example:-
www.foo.com/category-name/post-name.html
It will only work if the project url is same as that of the above.
www.foo.com/blog/category-name/post-name.html
This won't work you need to update the RewriteBase url accordingly l.
This is the conditions
RewriteEngine On
RewriteBase /
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule here
RewriteRule ^category-name/([0-9]+)-post-name$ /category-name/post-name.html?id=$1 [L]
This should work..
I have such rewrite rules
RewriteEngine On
RewriteCond %{HTTP_HOST} ^monkey.pl(.*) [NC]
RewriteRule ^(.*)$ http://www.monkey.pl/$1 [R=301,L]
RewriteRule ^horse.html$ /dog.html
and when I go to the monkey.pl/horse.html I get the message:
The requested URL /home/login/monkey/dog.html was not found on this server.
How can I get this to work. Basically what I'm trying to do is to change address of urls like:
http://www.monkey.pl/produkty.php?strona=1
to be displayed as
http://www.monkey.pl/produkty/czesci_do_mixokretow.html
but none of my rules are working. Therefore I'm trying to come with solution.
I tried many varations and I couldn't get it to work. I don't want to rewrite whole page. Just 6 pages which I need to change url and that's all. Fixed translation url => url.
If you are only doing a handful of URLs then you can do them this way.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{THE_REQUEST} /+produkty\.php\?strona=1 [NC]
RewriteRule ^ /produkty/czesci_do_mixokretow\.html [R=302,L]
RewriteRule ^produkty/czesci_do_mixokretow\.html$ /produkty.php?strona=1 [L]
RewriteRule ^horse\.html$ /dog.html [L]
I'm hoping someone can assist me with a mod_rewrite rule to redirect dynamic urls pointing to "videos.php" on my server to the base url.
For example, I need to redirect 'website.com/1/music/various/videos.php?=1234'
to 'website.com/videos.php?=1234'
edit: I am looking for a dynamic solution. If a url is pointed to videos.php at any time, I need to do a 301 redirect to home directory. Ie if /1/home/music/videos.php?=1234 redirect to /videos.php?=1234, or /music/playlist/1234/videos.php?1432 to /videos.php?1432.
Create a .htaccess file and insert these lines:
RewriteEngine On
# Page moved permanently:
RewriteRule ^videos\.php\?\=([0-9]+)\.html$ /1/music/various/videos.php?=$1 [R=301,L]
When testing, leave out the R=301, part, or you'll only see cache contents for a long, long time. Add it when sure its working fine. Or use 302, which means temporarily.
Please check this:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/1/music/various/videos.php$
RewriteCond %{QUERY_STRING} (.+)$
RewriteRule ^(.*) /videos.php [R,QSA]
RewriteCond %{REQUEST_URI} ^/videos.php$
RewriteCond %{QUERY_STRING} !(.+)$
RewriteRule ^(.*) http://%{HTTP_HOST}/ [R=301]
If it's not work, then try this:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/1/music/various/videos.php$
RewriteCond %{QUERY_STRING} ^(.+)$
RewriteRule ^(.*) /videos.php%1 [R]
RewriteCond %{REQUEST_URI} ^/videos.php$
RewriteCond %{QUERY_STRING} !(.+)$
RewriteRule ^(.*) http://%{HTTP_HOST}/ [R=301]
I need to rewrite a url so that it adds a url variable to all requests to the a sub-domain.
Example:
m.mydomain.com ==> m.mydomain.com?mobileFormat=true
This is the flag that triggers the mobile version of my site to load.
Should be easy right? Everything I have tried so far isn't working.
Here is the code I currently have in my .htaccess file:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteRule ^([a-zA-Z0-9/-]+)$ /index.cfm%{REQUEST_URI} [PT]
RewriteRule ^locale/([^/]*)$ /?siteLocale=$1 [L]
RewriteCond %{HTTP_HOST} ^m\.(.*)$
RewriteRule ^(.*)$ m.%1/$1?mobileFormat=true [QSA]
Thanks for the help.
I'm assuming that this is what you've tried:
RewriteCond %{HTTP_HOST} ^m\.(.*)$
RewriteRule ^(.*)$ m.%1/$1?mobileFormat=true [QSA]
QSA is Query String Append - if the URL they are trying to get to is:
m.mydomain.com?test=yes
then the above will rewrite this to:
m.mydomain.com?mobileFormat=true&test=yes
The solution I was looking for was:
RewriteCond %{HTTP_HOST} ^m\.(.*)$ [NC]
RewriteCond %{REQUEST_URI} !\.(xml|jpg|gif|png|js|css)$
RewriteRule .? $1?mobileFormat=true [QSA,L]
Thanks for all of the help.