RewriteRule doesn't work - apache

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]

Related

htaccess redirect if one query param matches

I'm trying to redirect from /somedir/index.php?action=something&id=x to /index.php?action=something&id=x
Only if action = something. id is dynamic.
Most recently I've tried this with no luck. What is wrong with this?
<IfModule mod_rewrite.c>
RewriteCond %{QUERY_STRING} (^|&)action=something($|&)
RewriteCond %{QUERY_STRING} (^|&)id=($|&)
RewriteRule ^somedir/index\.php$ /index.php?action?something&id=%2$ [NC,R]
</IfModule>
Note: somedir has an index.php and this rule in its htaccess. Will that result in conflicts?
RewriteRule ^.*$ index.php [NC,L]
Try this rule instead of your rule in .htaccess
<IfModule mod_rewrite.c>
RewriteCond %{QUERY_STRING} (^|&)action=something($|&)
RewriteCond %{QUERY_STRING} (^|&)id=x($|&)
RewriteRule ^somedir/index\.php$ /index.php?action=something&id=x [L,R=301]
</IfModule>
The result will not conflict.
You can use:
<IfModule mod_rewrite.c>
RewriteCond %{QUERY_STRING} (?:^|&)(action|id)=([^&]+)&(?:.*&)?(action|id)=([^&]+)(?:$|&) [NC]
RewriteRule ^somedir/index\.php$ /index.php?%1=%2&%3=%4 [NC,L,R=301]
</IfModule>

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]

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]

.htaccess RewriteRule for https specific prefix urls

Am wanting to create the https:// protocols on just these prefix urls on my site:
dream-portal.net/index.php?page=postratingspro
and
dream-portal.net/index.php?page=paypaltest
So anything in the URL after this should also include https:// protocol. For example:
dream-portal.net/index.php?page=postratingspro;sa=blahblah;testing
How can I do this? Have tried the following, but it doesn't turn all of my other urls to http:// on the site.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} = off
RewriteRule index.php?page=^(postratingspro|paypaltest)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L]
# don't do anything for images/css/js (leave protocol as is)
RewriteRule \.(gif|jpe?g|png|css|js)$ - [NC,L]
# force http for all other URLs
RewriteCond %{HTTPS} = on
RewriteCond %{REQUEST_URI} !page=^/(postratingspro|paypaltest)\$
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI} [R=302,L]
How can I accomplish this, what is wrong with my code for this?
Replace your code with this:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?page=(paypaltest|postratingspro)[&;,\s] [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{QUERY_STRING} !(^|&)page=(paypaltest|postratingspro)([,;&]|$) [NC]
RewriteRule ^(index\.php)?$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NC]

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]