URL query string not changing with .htaccess file - regex

I have this url: http://example.org/product.php?course_id=$x
And I want this url changes to this: http://example.org/Course-Name
this is my code in .htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ 404page.php
RewriteRule ^/([0-9]+)/([^/]+)/$ /product.php?course_id=$1 [L,R]
Which is not working kindly tell me whats wrong with my code?

You need to remove leading slash from your rule. Try this rule:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+product\.php\?course_id=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ /product.php?course_id=$1 [L,QSA]

Related

Modify code htaccess to accept parameters via URl

Modify code to accept parameters via URl. Rules to redirect URLs to www.site.com/acessorios.php tried are as follows, where .htaccess rules file is present along side with app folder.
RewriteEngine ON
RewriteBase /app/views/
##External redirect rules from here.
RewriteCond %{THE_REQUEST} \s/app/views/([^.]*\.php)\s [NC]
RewriteRule ^ /%1? [R=301,L]
##Internal rewrite rules from here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]*\.php)/?$ /app/views/$1 [QSA,L]
Samples:
www.site.com/accessories.php?id=1
www.site.com/accessories.php?id=1&name=Tiago
www.site.com/accessories.php?id=1&name=Tiago&image=foto.png
Samples:
www.site.com/accessories/1
www.site.com/accessories/1/Tiago
www.site.com/accessories/1/Tiago/foto.png
This order may be different
With your shown samples and attempts please try following .htaccess rules file. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /app/views/
##External redirect rules from here....
RewriteCond %{THE_REQUEST} \s/([^.]*)\.php\?id=(\d+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
RewriteCond %{THE_REQUEST} \s/([^.]*)\.php\?id=(\d+)&name=(\S+)\s [NC]
RewriteRUle ^ /%1/%2/%3? [R=301,L]
RewriteCond %{THE_REQUEST} \s/([^.]*)\.php\?id=(\d+)&name=(\S+)&image=(\S+)\s [NC]
RewriteRule ^ /%1/%2/%3/%4? [R=301,L]
##Internal rules from here onwards....
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/?$ /app/views/$1.php?id=$2&name=$3&image=$4 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/?$ /app/views/$1.php?id=$2&name=$3 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/?$ /app/views/$1.php?id=$2 [QSA,L]

Remove the query string from url using htaccess

I need some help with htaccess. Would you be so kind to assist a little bit?
I have a URL like this
https://example.com/index.php?p=application-intelligence
[or]
https://example.com/?p=application-intelligence
Basically the index.php is passed some parameter 'home' to know which page to load i.e. home.php
So I've tried to follow your post on your blog but with not much luck.
So I'd like the final code to be
https://example.com/application-intelligence
Here's my code.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?p=$1
Also for the
https://example.com?p=home
I'd like it to be just
https://example.com
You may use these rules:
RewriteEngine on
# handle /?p=home
RewriteCond %{THE_REQUEST} \s/+(?:index\.php)?\?p=home\s [NC]
RewriteRule ^ /? [R=301,L,NE]
# handle /?p=<something>
RewriteCond %{THE_REQUEST} \s/+(?:index\.php)?\?p=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?p=$1 [L,QSA]
This is what i have in my apache virtual host conf file:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?$1 [L,QSA]

Changing Dynamic URL through .htaccess

I'd like to change URL from http://example.com/?a=cust&page=abc to http://example.com/abc .
I tried the code mentioned below, but it got me stuck:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+\?a=cust&page([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /?a=cust&page$1 [L,QSA]
So any help on how to get it work correctly? :)
Thanks in advance.
You regex in first RewriteCond isn't correct. You can use:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+\?a=cust&page=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ ?a=cust&page$1 [L,QSA]
EDIT:
This is the new rule you want:
RewriteCond %{THE_REQUEST} \s/+\?a=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]

selectively grab portion of query string from url via url rewriting in htaccess

I have this in my .htaccess file:
RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.php\?p=(?!admin)(?!superadmin)((?![^&]*?edit)[^\s&]+) [NC]
RewriteCond %{THE_REQUEST} /index\.php\?p=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ index.php?p=$1 [L,QSA]
This rewrites in both directions. One way from:
domain.com/index.php?p=Home&l=en&m=0
to:
domain.com/Home
and vice versa. What I would like to do is preserve the query string part of the URL that comes after the value of 'p' and pass it along BOTH WAYS.
I was able to do one way, with this code:
RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.php\?p=(?!admin)(?!superadmin)((?![^&]*?edit)[^\s&]+) [NC]
RewriteCond %{THE_REQUEST} /index\.php\?p=([^\s&]+)&p=([^\s&]+) [NC]
RewriteRule ^ /%1?%2? [R=301,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ index.php?p=$1 [L,QSA]
but I can't get the other way right. Can you guys help? I basically want the value of p to be written as part of the URL after the domain and I don't want it to reappear again but I want all the other variables to be appended.
Right now what I get is:
domain.com/Home?m=0?&p=Home&ms=m0&l=en
This should work for you:
RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.php\?p=((?!(?:admin|superadmin|[^&]*?edit))[^\s&]+)(?:&(\S+))? [NC]
RewriteRule ^ /%1?%2 [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ index.php?p=$1 [L,QSA]

rewrite rule for subfolder index

i have this url
http://myurl.com/shiftturn/index.php?m=value
and i want:
http://myurl.com/shiftturn/value
this is my .htaccess
RewriteEngine On
RewriteRule /shiftturn/([^/]*)$ /index.php?m=$1 [L]
but doesn't work.
how can I do to get the kind of url I need, with a correct htaccess expression?
Place this rule in /shiftturn/.htaccess:
RewriteEngine On
RewriteBase /shiftturn/
RewriteCond %{THE_REQUEST} /index\.php\?m=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?m=$1 [L,QSA]