Apache rewrite rule with empty get args - apache

I need to redirect all urls with ? at the end. For example: http://example.com/abc?, but no http://example.com/abc?a=5 or http://example.com/abc.
This doesn`t work:
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)\?$ http://newdomain.com/$1 [R=301,L]
or
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} \?
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]

Try :
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} \?\sH
RewriteRule ^(.*)$ http://domain.com/$1? [R=301,L]

You can use this rule:
RewriteCond %{THE_REQUEST} \?\sHTTP
RewriteRule ^ %{REQUEST_URI}? [NE,R=301,L]

Related

MOD_REWRITE - URL ENDING TRAILING SLASH

Options -Indexes +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteCond %{HTTP_HOST} !^(.*)\.(.*)\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ HTTP%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
ServerSignature Off
RewriteRule ^cookie-policy/?$ cookie_policy.php [NC,L]`
Hello I'm struggling really hard with URL closing trailing slash.
RewriteRule ^cookie-policy/?$ cookie_policy.php [NC,L]
I would like, for example, to force a redirection from www.website.com/cookie-policy to www.website.com/cookie-policy/
Use this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]
It will add a / to end of your URLs. Make sure you clear your cache before testing this.

RewriteRule for .htaccess

I have this rule in .htaccess:
RewriteRule ^([^/]*)$ /user.php?username=$1 [L]
Output:
http://domain.tld/username
Desired output:
http://domain.tld/user/username
How can I do this?
Here is how to change a URL with a query string to a path:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/user.php [NC]
RewriteCond %{QUERY_STRING} ^.*username=([a-zA-Z]+).*$ [NC]
RewriteRule ^(.*)$ /user/%1? [L]
Input
http://domain.tld/user.php?username=bob
Output
http://domain.tld/user/bob
Here is how to go the other way:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/user/ [NC]
RewriteRule ^user/(.+)$ /user.php?username=$1 [L]
Input
http://domain.tld/user/bob
Output
http://domain.tld/user.php?username=bob
You can test .htaccess rules here: http://htaccess.madewithlove.be/
You can use this code in your DOCUMENT_ROOT/.htaccess file:
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^user/([^/]+)/?$ user.php?username=$1 [L,NC,QSA]

Simple .htaccess Rewrite of a Param

I'm trying to get a param to rewrite to a directory, nothing is happening.
Example of what I want:
Rewrite From: http://domain.com/results.php?s=abc
Rewrite To: http://domain.com/results/abc
This is what I have, there is a www redirect to non-www, and that part is working fine, but my param rewrite is doing nothing.
I'm on Apache 2.4.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteRule ^results/([^/]*)$ /results.php?c=$1 [L]
Ideas? Suggestions?
You can try:
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{THE_REQUEST} \s/+results\.php\?c=([^\s&]+) [NC]
RewriteRule ^ /results/%1? [R=301,NE,L]
RewriteRule ^results/([^/]+)/?$ results.php?c=$1 [L,QSA]

.htaccess RewriteRule - Difference between [L,R] and [R,L]?

Is there any difference between flag [L,R] and [R,L] e.g:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !newdomain.com$ [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [L,R=301]
vs
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !newdomain.com$ [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]
?
No there is no difference in 2 rules.
Order of various flags in RewriteRule doesn't matter.
Reference: httpd.apache.org/docs/current/rewrite/flags.html

htaccess rewrite url redirect php file to folder

I want to redirect this php url to the folder.
I always used the settings below. But since the update of apache to 2.4 it doesnt work anymore.
Goal:
example.com/about.php?sub=about or example.com/about.php?sub= should be seen in the browser as example.com/about/
I use the following settings.
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(example.com)(:80)? [NC]
RewriteRule ^(.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.example.com/$1/ [L,R=301]
RewriteRule ^about/$ about.php$1 [L]
RewriteRule ^about/([A-Za-z0-9-]+)$ about.php?sub=$1 [L]
This is what I am using now...
I don't see any rule in your code that is making /about.php?sub= to /about.
Try this code:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(about)\.php(\?sub=[^\s&]*)?\s [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteRule ^about/?$ about.php?sub=about [L,NC,QSA]
RewriteRule ^about/([a-z0-9-]+)/?$ about.php?sub=$1 [L,NC,QSA]
# Add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R]