RewriteRule for .htaccess - apache

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]

Related

htaccess redirect query string, but encoded ? to &

I want to redirect
https://www.aaa.com/query?v=abc123_ABC
to:
https://bbb.com/?url=https://www.aaa.com/query?v=abc123_ABC
I use this in .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ https://bbb.com/?url=https://www.aaa.com/$1 [L,B,NE,NC,QSA,R=301]
</IfModule>
But the output is:
https://bbb.com/?url=https://www.aaa.com/query&v=abc123_ABC
The ? was converted into &, what's wrong with the code?
Thanks.
====
You can test it here:
https://htaccess.madewithlove.be/
As per OP's request and shown samples could you please try following.
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s(/query)\?(v=abc123_ABC)\s [NC]
RewriteRule ^(.*)$ https://bbb.com/?url=https://%{HTTP_HOST}%1?%2 [NE,L]
The following rule should work.
RewriteEngine on
RewriteCond %{THE_REQUEST} /query\?v=.+\s [NC]
RewriteRule ^ https://bbb.com/?url=%{REQUEST_SCHEME}://%{HTTP_HOST}%{REQUEST_URI}?%{QUERY_STRING} [L,R]

Apache rewrite rule with empty get args

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]

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]

RewriteRule doesn't work

I want to convert this url:
http://ts.thebenamorgroup.com/g2g/category.php?cate=mens
to:
http://ts.thebenamorgroup.com/g2g/category/cate/mens/
I have this rule, but it doesn't work:
Options +FollowSymLinks
RewriteEngine on
RewriteRule category/cate/(.*)/ category.php?cate=$1
RewriteRule category/cate/(.*) category.php?cate=$1
Your rewrite is for the opposite direction. i.e. it rewrites
http://ts.thebenamorgroup.com/g2g/category/cate/mens/
to:
http://ts.thebenamorgroup.com/g2g/category.php?cate=mens
If what you want is to redirect
http://ts.thebenamorgroup.com/g2g/category.php?cate=mens
to:
http://ts.thebenamorgroup.com/g2g/category/cate/mens/
Then you can use:-
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} cate=(\d+) [NC]
RewriteRule ^g2g/category\.php$ /g2g/category/cate/%1/ [NC,R]
Try:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /g2g/
RewriteRule ^category/cate/(.+?)/?$ category.php?cate=$1 [L]
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /g2g/category\.php\?cate=([^&\ ]+)&?([^\ ]*)
RewriteRule ^ /g2g/category/cate/%2?%3 [L,R=301]

mod_rewrite not working for query string redirect

Currently I have one rewrite rule for pretty urls, which is working fine. I tried adding in a second, and it's giving me problems:
# redirect /?p=xyz to /xyz
RewriteCond %{QUERY_STRING} ^p=(.*)$
RewriteRule ^(.*)$ /index.php/%1 [L]
# Removes index.php from ExpressionEngine URLs
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
The second one is working fine, but the first one is giving me internal server error. Basically I want anything in ?p=* to go to /index.php/*
[Edit] Just to clarify, I need http://domain.com/?p=test to be equivalent to http://domain.com/test/
domain.com/test to domain.com/?p=test
Try this instead:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} !p= [NC]
RewriteRule ^([^/]+)/? /?p=$1 [L,NC]
OPTION:
In case it is the opposite:
domain.com/?p=test to domain.com/test/index.php
Try this:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} p=([^/]+) [NC]
RewriteRule .* /%1/index.php? [L,NC]
Remove index.php in case it is not needed, like this:
RewriteRule .* /%1/? [L,NC]