Rewrite rule then redirect - php

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]

Related

.htaccess; Rewrite folder name in URL

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]

Replace some parts of url with htaccess

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

Redirect one page to another with parameters via htaccess

I want to redirect from a page to another with parameters with htaccess.
For example:
From: www.websitename.co.uk/mylink.php?u=*username*
To: www.websitename.co.uk/mylink/*username*
I tried this but it's not working.
RewriteEngine on
RewriteRule www.websitename.co.uk/mylink.php?u=*username* www.websitename.co.uk/mylink/*username* [R=301]
Where am I doing wrong?
Working fine for me , hope this will work fine.
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/mylink\.php$
RewriteCond %{QUERY_STRING} u=([a-zA-Z]+)
RewriteRule .* http://websitename.co.uk/mylink/%1? [R=301]
Rewrite rules use regex like this:
RewriteEngine on
RewriteRule ^/mylink.php?u=(.*) /mylink/$1 [R=301]
Reference: https://httpd.apache.org/docs/current/rewrite/intro.html

htaccess cant get rewrite seo friendly url

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).

Htaccess 301 redirect one url to another with special characters doesnt work

I need to 301 redirect one URL to another. Now I have this rule in .htaccess
RewriteRule /forum/viewtopic.php?f=65&t=6 /page/name-of-the-page [R=301]
RewriteRule /forum/viewtopic.php?f=30&t=5 /page/another-name-of-the-page [R=301]
But it doesnt work at all. Could please anybody help me with that?
Thank you very much in advance!
You cant match query string using RewriteRule. Use it like this:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^f=65&t=6$
RewriteRule ^forum/viewtopic\.php$ /page/name-of-the-page? [R=301,L]
RewriteCond %{QUERY_STRING} ^f=30&t=5$
RewriteRule ^forum/viewtopic\.php$ /page/another-name-of-the-page? [R=301,L]
? in the end is used to strip-off any existing query string.
RewriteRule /forum/viewtopic.php?f=30&t=5 /page/another-name-of-the-page [R=301]
RewriteRule /forum/viewtopic.php?f=65&t=6 /page/name-of-the-page [R=301]
just reverse the order and it should work!

Categories