htaccess rule for redirecting when parameters are present - apache

RewriteEngine On
RewriteRule ^$ /index.phtml [R,L]
RewriteRule ^index.phtml.+$ /index.php$1 [R,L]
I have 2 urls like https://www.example.com/index.phtml and https://www.example.com/index.php which both point to very different sites. I want to redirect to index.php in case there are any parameters found after index.phtml.
For example, https://www.example.com/index.phtml?a=b should go to https://www.example.com/index.php?a=b (second rule). I am trying the above rules but the second one isn't matching (the first rule works fine). Thanks for any help/suggestions
Please ask if you need any more details.

You may use this code in site root .htaccess:
RewriteEngine On
# if there is a query string then redirect
# /index.phtml to /index.php
RewriteCond %{QUERY_STRING} .
RewriteRule ^(?:index\.phtml)?$ /index.php [L,NC]
# redirect landing page to /index.phtml
RewriteRule ^$ /index.phtml [R=302,L]
Note that QUERY_STRING get automatically appended to target URL so /index.phtml?a=b will be redirected to /index.php?a=b.
Make sure to clear your browser cache before testing this change.
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your redirect rules.

With your shown samples, please try following .htaccess rules file. Please make sure that your htaccess file and index.php are present in same folder.
Also make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##For without query string rules.
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^index\.phtml/?$ index.php [R=301,NC,L]
##For query string rules.
RewriteCond %{THE_REQUEST} \s.index\.phtml\?([^=]*=\S+)\s [NC]
RewriteRule ^ index\.php?%1 [R=301,L]

Related

301 Redirect in htaccess for URLs having Parameter P

I need to set 301 redirect in htaccess for URLs having parameter P. One example URL is
http://www.price4india.co.in/vivo-x20-plus-ud-price-in-india-scanner-feature-real.html?p=1028
to
http://www.price4india.co.in/vivo-x20-plus-ud-price-in-india-scanner-feature-real.html
After redirect everything after .html shall get removed and the value after P=...... can be any numerical value. So far I have tried below query but it is not working. Any suggestion please...
RewriteCond %{QUERY_STRING} ^p(&|$) [NC]
RewriteRule ^ %{REQUEST_URI}? [R=301,L]
With your shown samples, please try following .htaccess rules file. Make sure to keep your .html file and .htaccess files in root path only.
Make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/(.*\.html)\?p=\d+\s [NC]
RewriteRule ^ /%1? [R=301,L]
NOTE: In case you have further more rules in your .htaccess rules, which includes internal rewrite of html files then you could keep these rules above those.
RewriteCond %{QUERY_STRING} ^p(&|$) [NC]
RewriteRule ^ %{REQUEST_URI}? [R=301,L]
This is almost correct, except the regex ^p(&|$) is incorrect. This matches p&<anything> or p exactly. Whereas you need to match p=<anything> (eg. ^p=) or p=<number> (eg. ^p=\d+). This is of course assuming the p URL parameter always occurs at the start of the URL-path (as in your example).
For example:
RewriteCond %{QUERY_STRING} ^p= [NC]
RewriteRule ^ %{REQUEST_URI}? [R=301,L]

What is the exact process for URL rewriting and redirecting in htaccess?

I am completely new to building websites and want to understand the process for URL rewriting and redirecting as the rules I am including in my htaccess files are not all working and I feel I am missing sth very obvious in here.
I have tried to read Apache documentation however i just can't fully understand it being new to coding - hopefully someone will be able to point me in the right direction.
I have rewritten my URL so I could change the way the URL is displayed in the browser and make it more user/SEO friendly - basically removed .html using below code.
RewriteEngine On
RewriteRule ^skills$ skills.html [NC,L]
The rule above is working perfectly and displaying nice clean URL.
Now to avoid duplicated URL's (Screaming Fog shows now duplicated URL: skills and skills.html) I want to do 301 redirect with the below:
Redirect 301 /skills.html https://www.youmedigital.com/skills
The problem I am getting now is: too many redirects, across all browsers. I understand that this issue usually happens when you are redirected from the original URL to a new one but unfortunately fall in to an infinite redirect loop. I tried to fix it by clearing my cookies as per online tutorials but it doesn't help.
Can you please help me to understand:
1. Do I need to first rewrite URL before 301 redirecting it as per above? or there is no need to rewrite URL first and just do 301 redirect - I tried both solutions and none of them worked.
2. Do my rules above are correct as i have feeling that maybe the error I'm getting just shows i messed up.
I have included below everything I have in my htaccess file:
#Rewrite everything to https
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#301 redirect to www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^youmedigital.com [NC]
RewriteRule ^(.*)$ http://www.youmedigital.com/$1 [L,R=301]
#301 rewrite url and 301 redirect for clean URL
RewriteEngine On
RewriteRule ^skills$ skills.html [NC,L]
Redirect 301 /skills.html http://www.youmedigital.com/skills
#remove index.html and kill the chance of homepage having duplicates
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.(php|html) [NC]
RewriteRule ^index\.html$ http://www.youmedigital.com/ [R=301,L]
#remove php and kill the chance of having duplicates
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Thank you for the help in advance!
Let me go throug your code then show you why you get looping :
RewriteEngine On
RewriteRule ^skills$ skills.html [NC,L]
line above will match this request http://yourdomain.com/skills so , it will be redirected to http://yourdomain.com/skills.html just adding extension.
At your second rule :
Redirect 301 /skills.html https://www.youmedigital.com/skills
What you do is match this request http://yourdomain.com/skills.html so , it will be redirected to http://yourdomain.com/skills so , i think right now you know why looping happened , when first rule applied the second will start and vise versa.
To avoid that , try this :
RewriteEngine On
RewriteCond %{THE_REQUEST} /skills\.html [NC]
RewriteRule ^ /skills [NC,L,R=301]
RewriteRule ^skills$ /skills\.html [L]
That code above will catch http://yourdomain.com/skills.html request and redirect it to http://yourdomain.com/skills externally then redirect http://yourdomain.com/skills request to http://yourdomain.com/skills.html but internally .
Clear your browser cache then test it .

.htaccess get lost query string

I have limited .htaccess knowledge and i am requiring some help. I need to do some redirects to enable pretty urls.
In local all works fine but it is not working in another develpment server. apparently the query string get drop when redirect.
i need to redirect this http://mysite.local/info/permalink/1
to this one http://mysite.local/info?proxy=true&id=1
my .htaccess code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#remove /index.php from home url and redirect to root.
#http://mysite.local/index.php -> http://mysite.local/
RewriteCond %{THE_REQUEST} ^.*\/index\.php?\ HTTP/
RewriteRule ^(.*)index\.php?$ "/$1" [R=301,L,QSA]
#pretty url without index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [PT,QSA,L]
#rewrite to handle some permalink saved on my db.
#http://mysite.local/info/permalink/1
#http://mysite.local/info/proxy=true&id=1
RewriteRule ^([^/]+)/info/([a-zA-Z0-9\-]+)/([0-9]+) /info/?proxy=true&id=$3 [L]
</IfModule>
the redirect is working but the query string is not present. When I run var_dump($_GET) on info module i am getting an empty array array(0) {}
i have try it to solve changing
RewriteRule .* index.php [PT,QSA,L]
to RewriteRule ^(.*)$ /%{QUERY_STRING} [L]
and
RewriteRule ^([^/]+)/info/([a-zA-Z0-9\-]+)/([0-9]+) /info/?proxy=true&idobj=$3 [L]
to
RewriteRule ^([^/]+)/info/([a-zA-Z0-9\-]+)/([0-9]+) /info/?proxy=true&idobj=$3 [QSA,NE,L]
Server API is CGI/FastCGI
What should I change to ensure that my rewrite works as intended and $_GET variables still are accessible?
thx in advance
I have no idea how you've managed to get this to work with a regex pattern like: ^([^/]+)/info if the URL you are going to is /info/permalink/1.
The ^([^/]+)/info pattern means there's stuff before the /info part of the URI, which there isn't. Additionally, in an htaccess file, the URI's have the leading forward slash stripped off. So you probably want something like this:
RewriteRule ^info/([a-zA-Z0-9\-]+)/([0-9]+) /info/?proxy=true&id=$2 [L]

Can't get mod_rewrite to correctly (and invisibly) re-write my URLs?

I'm trying to make a URL shortening service for my website.
So instead of:
http://www.myfullwebsitename.com/page78/this-is-a-headline/
users will be able to visit:
http://abc.de/aBxf
which needs to redirect (invisibly!) to
http://abc.de/?shorturl=aBxf
which then 301 redirects via a database lookup to
http://www.myfullwebsitename.com/page78/this-is-a-headline/
I can do the DB lookup and the 301 redirect easily. It's the invisible intermediate redirect that I'm struggling with.
I've tried a LOT of different things, but none seems to work. This is what I currently feel should work:
RewriteCond %{HTTP_HOST} ^abc.de
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^/(.+) /?shorturl=$1
But instead of redirecting silently to
http://abc.de/?shorturl=aBxF
it redirects "noisily" (302) to
http://abc.de/aBxF/?shorturl=aBxF
What am I doing wrong?
Thank you!
There's a few things you can try.
I think your RewriteRule should look like this (without the forward /):
RewriteRule ^/(.+) ?shorturl=$1 [L]
This should at the very least stop it from redirecting to http://abc.de/aBxF/.
Your original rule may work if you add:
RewriteBase /
If it were me my rules would actually look like this:
RewriteBase /
RewriteCond %{HTTP_HOST} ^abc.de$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /redirect.php [L]
And then in PHP I would use $_SERVER['REQUEST_URI'] to get the URL (not sure what language you're using).
The rule can look like this:
RewriteRule ^(.*)$ /redirect.php?shorturl=$1 [L]
But I would make sure to mention the script by name. Part of what may be throwing your rules off is relying on Apache finding your index file after a rewrite.
The way Apache's rewrite rules work is as soon as the URL is rewritten, it actually will re-run the rules until no other rules will be found. The [L] flag for "last" says "stop here" - but it still starts over from the top. The RewriteCond with the !-f flag says "only if the file doesn't exist".
Use an absolute URL:
RewriteCond %{HTTP_HOST} ^abc.de
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*)$ http://abc.de/?shorturl=$1 [R=301,L]

mod_rewrite: remove www and redirect into folder if not already in URL

First time here, and I'm a bit of a mod_rewrite and regex noob, so please let me know if I've missed anything. I've searched here and elsewhere all day but not found exactly what I need.
I'm looking for a set of RewriteConds and RewriteRules that will accomplish the following:
remove the www, if present in URL
redirect into a folder, if not already present in URL
preserve the rest of the URL
I have a web app installed in a specific subfolder (we'll call it /webapp) which is configured to require no www on the url. It presents a stupid annoying message to the user if they include the www. I can dig into the app and reprogram that, but I'd like to handle that for the users through .htaccess and mod_rewrite, and simultaneously dump them into the folder, if they forget to type it, doing all of this with a 301 redirect.
For example, I'd like any of the following requests
http://www.mydomain.org/webapp/anything
http://www.mydomain.org/anything
http://mydomain.org/anything
To be redirected to
http://mydomain.org/webapp/anything
And obviously if a "correct" URL (one starting with http://mydomain.org/webapp/) is requested, it's not rewritten at all.
My best guess so far is as follows:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.mydomain\.org$ [NC]
RewriteRule ^(.*)$ http://mydomain.org/$1 [R=302]
RewriteCond %{REQUEST_URI} !^/webapp.*$ [NC]
RewriteRule ^(.*)$ http://mydomain.org/webapp/$1 [R=302]
This seemed to work according to http://htaccess.madewithlove.be/ but in practice, not so much.
Thanks in advance.
try:
RewriteCond %{HTTP_HOST} ^www\.mydomain\.org$
RewriteRule ^ mydomain.org/$1 [L,R=301]
RewriteCond %{REQUEST_URI} !^/webapp.*$
RewriteRule ^/(.*) mydomain.org/webapp/$1 [L,R=301]
Found the answer myself here: BowlerHat
Looks like this:
# First just remove the www
RewriteCond %{HTTP_HOST} ^www\.mydomain\.org$ [NC]
RewriteRule ^(.*)$ http://mydomain.org/$1 [L,R=301]
# Now redirect into the folder
RewriteCond %{REQUEST_URI} !webapp/ [NC] # if the provided URI does not start with /webapp,
RewriteRule (.*) http://mydomain.org/webapp/ [L,R=301] # redirect user to /webapp/ root
I decided that if users try to visit mydomain.org/somethingsomething, just to send them to the root of the webapp, rather than /webapp/somethingsomething.