Changing Dynamic URL through .htaccess - regex

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]

Related

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]

htaccess Rewrite for any domain

I have the following lines written multiple times to accommodate the multiple domains we use. Would there be a way to write this once so any domain would follow the same rule?
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteCond %{THE_REQUEST} \s/+example.com/(\S*) [NC]
RewriteRule ^ /%1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/example.com/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /example.com/$1 [L]
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ example.com/index.php [L]
Try this single block of rule for all the doamins:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteCond %1::%{THE_REQUEST} ^(.+?)::\s/+\1/(\S*)
RewriteRule ^ /%1 [R=301,L]
RewriteCond %{HTTP_HOST}::%{REQUEST_URI} ^(?:www\.)?(.+?)::/(?!\1/) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ %1/$1 [L]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^/?$ %1/index.php [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]

URL query string not changing with .htaccess file

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]

Virtual Subdomains

I want to redirect wildcard subdomains to the file "user_site.php" like the following :
RewriteCond $1 !^(user_site\.php|home_site\.php
RewriteCond %{HTTP_HOST} !^(www|mail|ftp)?\.?domain.com [NC]
RewriteRule ^(.*)$ /user_site.php/$1 [L]
but if the subdomain is www or empty , i want to rewrite it to "home_site.php"
how can this be done ?
Thanks
I've found it already answered here on stackoverflow , Thanks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(www\.)?domain.com$ [NC]
RewriteRule ^(.*)/?$ home_site.php/$1 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_URI} ^/index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !^(www|mail).domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+).domain.com$ [NC]
RewriteRule ^(.*)/?$ user_site.php/$1 [NC,QSA,L]