I've many links like this:
www.example.com/my_folder/something/33
www.example.com/my_folder/anything/81
How can I rewrite my_folder to folder123.
Means: If I type www.example.com/folder123/something/33 I'm getting the content of www.example.com/my_folder/something/33 but the URL remains www.example.com/folder123/something/33.
How can I do that with .htaccess?
You can do this with a simple rewrite rule as this:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+my_folder(/\S*)?\s [NC]
RewriteRule ^ /folder123%1 [R=301,L,NE]
RewriteRule ^folder123(/.*)?$ my_folder$1 [L,NC]
Related
I want to replace some parts of url using htaccess.
I am using below code but i am getting 404 error.
RewriteRule ^/v1/surveys/login(.*)$ /oauth2/rest/token/$1 [R=301,L]
This is my url http://192.168.1.10/survey/api/v1/surveys/login and i want to
replace with http://192.168.1.10/survey/api/oauth2/rest/token
You can use this rule just below RewriteEngine On line:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/(survey/api)/v1/surveys/login(/.*)?$ [NC]
RewriteRule ^ /%1/oauth2/rest/token%2 [R=301,L,NE]
# rest of the rules go below this
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...
hi i have use many different htaccess codes, but i cant get rewrite to work.
i want this url
https://domain.com/category.php?cat=firm
to look like this url
https://domain.com/category/firm
this is my latest attempt
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([^/]*)\.html$ /category.php?cat=$1 [L]
ErrorDocument 404 https://seoboost.no/404page.php
RewriteCond %{HTTP_HOST} ^www\.seoboost\.no$
RewriteRule ^/?$ "https\:\/\/seoboost\.no\/" [R=301]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^/?$ "https\:\/\/example\.com\/" [R=301,L]
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* ? [F]
RewriteRule ^index\.php$ / [R=301]
RewriteRule ^(.*)/index\.php$ /$1/ [R=301]
i have try to delete all in my htaccess and only have this code
RewriteEngine on
RewriteRule ^([^/]*)\.html$ /category.php?cat=$1 [L]
but it still not working, is my server or what am i doing wrong??
Try replacing your .htaccess with
RewriteEngine On
RewriteRule ^category/(.*)$ /category.php?cat=$1 [L]
which will redirect yoursite.com/category/12 to yoursite.com/category.php?cat=12 internally, and the user will never see the "ugly" url. Then inside your category.php file you can access the cat by $category_id = $_GET['cat']
A simple anchor tag looks like this:
Item 12
Pulling from Justin Lurman's answer here, the following should work:
RewriteEngine On
# redirect "/category.php?cat=..." to "/category/..." to hide a directly accessed "ugly" url
RewriteCond %{THE_REQUEST} \s/category\.php\?cat=(.+)\s [NC]
RewriteRule ^ /category/%1? [R=301,L]
# internally rewrite "/category/..." to "/category.php?cat=..."
RewriteRule ^category/(.+)$ /category.php?id=$1 [L]
As Justin noted in that answer to a similar question, you need to confirm that htaccess files are enabled/allowed in your Apache configuration and that mod_rewrite is enabled.
Additionally, I am sure you are aware, but just in case you are not, after implementing this new htaccess redirect rule, you should change all of the links on your webpages to use clean/friendly URLs. By doing so, neither your users nor a search engine crawler will access the "ugly" URLs unless they access them directly (via a bookmark or a saved link).
Hello I am trying to rewrite my url
http://example.net/?r=123
to this one
http://example.net/ref/123
In my .htaccess this is working using this code
RewriteEngine on
RewriteRule ^ref/([^/.]+)/?$ /index.php?r=$1 [R=301]
But i would like to add another rewrite rule. When the link is like this:
http://example.net/?r=123
I would like to redirect it to:
http://example.net/
I tried this rewrite rule but it's not working:
RedirectMatch "^/?r=([^/.]+)" "/index.php"
Please help. Any help will be very much appreciated. Thank you so much.
Try these rules to redirect the query string URL to the pretty URL which is more common. Basically redirecting the old url to the SEO friendly one.
RewriteEngine on
RewriteCond %{THE_REQUEST} ^GET\ /+\?r=([^&\s]+)\
RewriteRule ^ /ref/%1? [R=302,L]
RewriteRule ^ref/([^/.]+)/?$ /index.php?r=$1 [R=301]
If you still want to redirect the query string to index.php instead try this rule
RewriteEngine on
RewriteCond %{THE_REQUEST} ^GET\ /+\?r=([^&\s]+)\
RewriteRule ^ /index.php [R=302,L]
RewriteRule ^ref/([^/.]+)/?$ /index.php?r=$1 [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