htaccess rewrite - apache

I am working on a project and it utilizes a shortened url for redirection, I've gotten the following code to work perfectly:
XBitHack Off
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{REQUEST_URI} \/([0-9A-Za-z]{4})$ [NC]
RewriteRule ^(.*) ./getdeck.php?deck=%1 [L]
Now I want to add a second redirect rule for /a/([0-9A-Za-z]{4} but no matter how I write it, the new rule fails. Can anyone help explain what I'm missing?
Examples I have tried:
RewriteCond %{REQUEST_URI} ^/a/.*$ [NC]
RewriteRule ^(.*) ./deck/makedeck.php?deck=%1 [L]
or
RewriteCond %{REQUEST_URI} ^/a/([0-9A-Za-z]{4})$ [NC]
RewriteRule ^(.*) ./deck/makedeck.php?deck=%1 [L]

You might have to repeat the first two conditions.

Related

RewriteRule - remove params keys and keep values in address bar

RewriteEngine ON
RewriteRule ^video$ video.php [L]
the above line works
example.com/video is interpret as example.com/video.php
now I need example.com/video?id=5&s=lorem-ipsum
to interpret as example.com/video/5/lorem-ipsum - and vice versa
RewriteCond %{THE_REQUEST} video?id=([^\s&]+)&s=([^\s&]+) [NC]
RewriteRule ^ %1/%2? [R=301,L]
RewriteRule ^([\w-]+)/([\w-]+)/?$ video?id=$1&s=$2 [L,QSA]
RewriteRule ^([\w-]+)/([\w-]+)/?$ video?/$1/$2 [L,QSA]
doesn't work - in both directions
pls help
With your shown attempts, please try following .htaccess rules file. These rules are assuming you want to infernally rewrite to index.php you can change php file name in 2nd set of rules as per your requirement.
Make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##External redirect Rules from here..
##Redirect 301 to example.com/video/5/lorem-ipsum from example.com/video?id=5&s=lorem-ipsum
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} \s/(video)\?id=([^&]*)&s=(\S+)\s [NC]
RewriteRule ^ /%1/%2/%3? [R=301,L]
##Internal rewrite for example.com/video?id=5&s=lorem-ipsum
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^video/([^/]*)/(.*)/?$ index.php?id=$1&s=$2 [QSA,NC,L]

How to remove parameter from URL

I apologise if this question is asked before but I can't find the specific instance for my problem.
How to achieve this www.example.com/index.php?user=john to www.example.com/john
I am using this already to remove the .php extension but it doesn't fix the user parameter.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R]
ErrorDocument 404 /error.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/([A-Za-z0-9_]+) ?user=$1
I highly value any reply. Please help.
With your shown attempts, please try following htaccess rules. Considering that you have to pass your arguments to index.php file in backend.
Also make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##External redirect rules here.
RewriteCond %{THE_REQUEST} \s/index\.php\?user=(\S+)\s [NC]
RewriteRule ^ /%1? [R=301,L]
##Internal rewrite rules here..
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?$ index.php?user=$1 [QSA,L]

Why isn't my htaccess RewriteRule working for my query strings?

I am moving an ASPX site to Wordpres -- from an IIS server to Apache -- and I am having problems getting the pages with query strings to redirect. I looked into the Apache documentation and am trying to use mod_rewrite, but so far no luck.
For example I would like to redirect these pages:
www.mysite/Product/Product.aspx?mapid=324$ to
www.mysite/blue-widgets/widget7/
www.mysite/Product/Product.aspx?mapid=681$ to
www.mysite/blue-widgets/widget12/
www.mysite/Product/Product.aspx?mapid=841$ to
www.mysite/blue-widgets/widget82/
Here is where I am at. I have tried several iterations of this but can't get it to work. I am starting to think that it might have something to do with first few Rules, which were generated by the server and Wordpress. The site is an addon domain, which the server treats like a subdomain.
My rewrites are the last three. Any ideas why these aren't working? Thanks for any help.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /~username/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /~username/mysite/index.php [L]
RewriteCond %{QUERY_STRING} ^mapid=324$ [NC]
RewriteRule ^/Product/Product\.aspx(.*)$ ^https://www.mysite/blue-widgets/widget7/? [L,R=302,NC]
RewriteCond %{QUERY_STRING} ^mapid=681$ [NC]
RewriteRule ^/Product/Product\.aspx(.*)$ ^https://www.mysite/blue-widgets/widget12/? [L,R=302,NC]
RewriteCond %{QUERY_STRING} ^mapid=841$ [NC]
RewriteRule ^/Product/Product\.aspx(.*)$ ^https://www.mysite/blue-widgets/widget82/? [L,R=302,NC]
</IfModule>
I believe the following will work:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^mapid=324$ [NC]
RewriteRule /Product/Product\.aspx https://www.example.com/blue-widgets/widget7/? [L,R=302,NC]
RewriteCond %{QUERY_STRING} ^mapid=681$ [NC]
RewriteRule /Product/Product\.aspx https://www.example/blue-widgets/widget12/? [L,R=302,NC]
RewriteCond %{QUERY_STRING} ^mapid=841$ [NC]
RewriteRule /Product/Product\.aspx https://www.example/blue-widgets/widget82/? [L,R=302,NC]
RewriteBase /~username/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /~username/mysite/index.php [L]
To better explain for others, the order of the rules is important. Since the Wordpress rules change the RewriteBase and change the URL the rules for the .aspx pages will never be hit if they are at the bottom.

htaccess condition and rewrite

i have this link
http://localhost/fukhtaccess
and i want redirect the link to http://server.localhost/index.php with GET params.
this is my htaccess, but don't work and not possibile test with some error for debug (thanks apache foundation!!!)
RewriteEngine On
RewriteCond %{HTT_HOST} ^localhost$
RewriteCond %{REQUEST_URI} fukhtaccess
RewriteRule (.*) http://server.localhost/index.php [QSA,L]
You have a typo: change HTT_HOST to HTTP_HOST.
Also, your second RewriteCond just complicates things unnecessarily. Do this instead:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^localhost$
RewriteRule fukhtaccess http://server.localhost/index.php [QSA,L]
better, this work fine
RewriteEngine On
RewriteCond %{HTTP_HOST} ^localhost [NC]
RewriteCond %{REQUEST_URI} fukhtaccess$ [NC]
RewriteRule (.*) http://server.localhost/index.php [QSA,L]

My htaccess rewrite doesnt work

I have a folder structure like this
/localhost/parent/
I have an .htaccess residing in
/localhost/parent/
I want to rewrite everything from /localhost/parent/WebContent/ to /localhost/parent/
here is my code
RewriteEngine on
RewriteRule ^$ WebContent/$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^WebContent/
RewriteRule ^(.*)$ WebContent/$1
For some reason the first two lines work fine, but the rest doesn't work. So I am able to redirect only homepage.
Update
RewriteBase /parent/
RewriteEngine on
RewriteRule ^$ WebContent/$1
#RewriteCond %{REQUEST_URI} !WebContent
RewriteRule (.*) /parent/WebContent/$1 [R=301,L,QSA]
Goes into an infinite loop. I just need to avoid that loop now
Try switching the order of the lines so that
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
comes before everything else.
You could also try adding this line:
RewriteBase /WebContent/
I used this
RewriteEngine on
RewriteRule ^$ /parent/WebContent/$1
RewriteCond %{REQUEST_URI} !WebContent
RewriteRule ^(.*)$ /parent/WebContent/$1