%{REQUEST_URI} doesn't seem to work with index.php - regex

I have a website that uses the following format of links:
http://www.website.com/section1/index.php
http://www.website.com/section2/index.php
http://www.website.com/section3/index.php
http://www.website.com/section1/section4/index.php
What I was trying to do is to get rid of the last part "index.php" by using the following .htaccess directives:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/index\.php$
RewriteRule (.*) http://www.google.com [R=301,NC,L]
Of course, www.google.com is just for testing purposes, however the example below doesn't work. What is wrong with it? The second part of the question is what do I replace www.google.com with if I want to rewrite to http://www.website.com/section1 ?
Thank you!

It is not working because your regex is incorrect. RewriteCond %{REQUEST_URI} ^/index\.php$ is expecting %{REQUEST_URI} to be /index.php but you have /section1/index.php.
Correct version will be:
RewriteEngine on
RewriteCond %{REQUEST_URI} /index\.php$
RewriteRule (.*) http://www.google.com [R=301,NC,L]
Or even:
RewriteEngine on
RewriteRule /index\.php$ http://www.google.com [R=301,NC,L]

Related

Redirection through Regular expression oscthumb.php query string unable

I am unable to solve this one. I need to redirect this kind of urls:
http://www.example.com/oscthumb.php?src=/images/image.jpg
or
http://www.example.com/oscthumb.php?src=/images/image.jpg&w=150&h=131.8125&f=jpg&q=95&hash=317ec2edb3fa82e56787a5f3308d5e96
to
http://www.example.com/images/image.jpg
without parameters. Image.jpg can vary
How can I do that in .htaccess?
I tried this but it doesn't work properly:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/oscthumb\.php$
RewriteCond %{QUERY_STRING} ^src=(.*)$
RewriteRule ^(.*)$ http://example.com/%1 [R=301,L]
Try to use this code:
$url = $_GET['src'];
header('Location: http://www.example.com/' . $url);
exit;
EDIT:
.htaccess:
RewriteEngine On
RewriteCond %{REQUEST_URI} \.jpg
RewriteRule =([^=]+\.jpg) http://example.com$1 [R=301,L]

Apache Rewrite Issue. Unable to figure out query string

So I have been struggling on finding the rule to match this rewrite. I am working on a client website and it is a nightmare with the number of duplicate title tags. I have managed to resolve most of them by enforcing forward slash, redirect non www. to the www. version and disallow crawling of https version of the website.
The issue I am having at the moment. I have over 1000 URLs that are duplicate content, each product has two different URLs with the exact same content. An example is:
http://www.example.co.uk/product/widget1/
http://www.example.co.uk/widget1/
http://www.example.co.uk/product/widget2/
http://www.example.co.uk/widget2/
Now the following URLs have the same content:
http://www.example.co.uk/product/widget1/
http://www.example.co.uk/widget1/
I want to redirect any URL that contains "/product/" to the URL version without "/product/" in the URL if that makes sense. I honestly don't know where to start and would really appreciate the help.
Thanks in advance
EDIT: The recommended rule:
RewriteEngine On
RewriteRule ^/product/(.*)$ /$1 [R=301]
does not work. It may be conflicting. These are the other rules:
RewriteEngine On
RewriteCond %{QUERY_STRING} .+
RewriteRule ^(.*)$ /$1? [R=301,L]
RewriteRule ^/product/(.*)$ /$1 [R=301]
RewriteCond %{HTTP_HOST} ^example\.co [NC]
RewriteRule (.*) http://www.example.co.uk/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)([^/])$ /$1$2/ [L,R=301]
I dont know if there are any conflicts here. Please help
Have your full .htaccess like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.co [NC]
RewriteRule (.*) http://www.example.co.uk/$1? [L,R=301]
RewriteCond %{QUERY_STRING} .+
RewriteRule ^(.*)$ /$1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?[^/])$ /$1/ [L,R=301]
RewriteRule ^product/([^/]+)/?$ /$1/ [R=301,L]
Assuming the URLs always start with product, this should work:
RewriteEngine On
RewriteRule ^/product/(.*)$ /$1 [R=301]
It'll need to go in your main site conf or .htaccess

String start with and end with anything (but is not .php)

Trying to make a redirection URL in htaccess.
I want to redirect URLs like
www.domain.com/pageANYTHING
to
www.domain.com
But I have an exception : when I got this
www.domain.com/page.phpANYTHING
do nothing.
Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^\.phpANYTHING [NC]
RewriteRule ANYTHING$ http://www.domain.com/? [L,R,NC]
Try this:
RewriteEngine On
RewriteRule ^page(?!\.php).*$ / [R=301,L]
In fact It works with a mix of your two propositions.
RewriteCond %{REQUEST_URI} !\.(?:php)$
RewriteRule ^page(?!\.php).*$ / [R=301,L]
Thank you so much guys.

Redirect all to base domain

I would like to redirect all pages like:
www.mydomain.com/test
www.mydomain.com/test2/test3
and so on ...
to always base
www.mydomain.com
How can i do this?
RewriteCond %{HTTP_HOST} ^www.mydomain.com
RewriteCond %{THE_REQUEST} ^/(.*)$
RewriteRule (.*) http://%{HTTP_HOST} [L,QSA,R=301]
wont work
RedirectMatch 301 ^/ http://www.mydomain.com/
It will redirect everything to your new domain.
This will work if you have mod_alias
Please try this :
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/([a-z].*)
RewriteRule .* http://kap.com/ [R,L]
I find that solution OK
RewriteCond %{HTTP_HOST} ^www.mydomain.com
RewriteCond %{REQUEST_URI} ^/[a-zA-Z0-9\/]+$
RewriteRule ^ / [R,L]
Just leave out the RewriteConds and redirect everything to /
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^ / [R,L]
When everything works as you expect, you can change R to R=301.
Never test with 301 enabled, see this answer Tips for debugging .htaccess rewrite rules for details.

regex in my RewriteRule is OK, but .htaccess does not work

I'm having a lot of trouble getting my .htaccess ReWrite to work on my apache web server. I've read several tutorials and tested my regex matching with Grep.
Here is the code:
RewriteRule \?action=viewArticle&articleId=([0-9]*)&categoryId=([0-9])$ essays/$1 [R=301,L]
here is a url I'm trying to match:
http://mysite.com/?action=viewArticle&articleId=15&categoryId=1
and change to
http://mysite.com/essays/15
UPDATE: Solution! with a very excellent tutorial from Jon. It was very important that I put <base href="/"> in my header file to get the css to work correctly.
Final rewrite looked like this:
RewriteCond %{THE_REQUEST} /?action=viewArticle&articleId=([0-9]*)&categoryId=([0-9])
RewriteRule ^$ /essays/%1? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^essays/([0-9]+) /?action=viewArticle&articleId=$1&categoryId=([0-9]) [L]
You can't match hosts or query strings inside a RewriteRule, you need to match against the %{HTTP_HOST} and %{QUERY_STRING} variables in a RewriteCond directive:
RewriteCond %{HTTP_HOST} mysite\.com$ [NC]
RewriteCond %{QUERY_STRING} ^action=viewArticle&articleId=([0-9]*)&categoryId=([0-9])$
RewriteRule ^$ /essays/%1? [L,R=301]
This redirects the browser (changing the URL in the address bar) when someone goes to http://mysite.com/?action=viewArticle&articleId=15&categoryId=1 to http://mysite.com/essays/15
You don't need the %{HTTP_HOST} condition if your htaccess file only serves a single host.