Htaccess redirection, redirect loop - .htaccess

I would like to redirect mysite.com/index.php to mysite.com/index.php?id_category=12&controller=category
Here is my htaccess file.
But it doesn't work. Chrome says : This webpage has a redirect loop.
Options +FollowSymLinks +SymLinksIfOwnerMatch
RewriteEngine On
RewriteBase /
RewriteRule index\.php http://mysite.com/index.php?id_category=12&controller=category

This should work:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/index.php$
RewriteCond %{QUERY_STRING} ^$
RewriteRule (.*) http://www.mywebsite.com/index.php?foo=bar [R=301,L]
The first condition checks if the URI is equal to index.php and the second one checks if GET values are empty. The AND between the 2 conditions is implicit here.

Probably you should change use another file name instead of index.php for the destiny. But you can try:
RewriteRule index.php index.php?id_category=12&controller=category

Related

url rewrite preserve get variables

I am looking to rewrite
http://domain.com/zzz-zzz/?source=[variable1]&referrer=[variable_2]
to
http://domain.com/xxx-xxx/?source=[variable1]&referrer=[variable_2]
I had done :
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} ^(.*)zzz-zzz(.*)$
RewriteRule ^$ ?xxx-xxx%2 [R=301,L]
The QUERY_STRING does not include the path and script file (zzz-zzz/). That would be the REQUEST_URI. The QUERY_STRING would be source=variable1&referrer=variable2. If all you want to do is change zzz-zzz to xxx-xxx:
RewriteEngine On
RewriteBase /
RewriteRule ^zzz-zzz/?(.*)$ /xxx-xxx/$1 [R=301,L]
ought to get you close. The entire Query String should be brought over.

htacess redirect if url stops with specific word

Is it possible to redirect based on an url that would be as:
www.url.com/stuff.php
But not redirect if the url contained any characters after the php, as such:
www.url.com/stuff.php?variables=values&othervariable=othervalue&etc=more
I have no clue how to wriite this either.
Thanks in advance!
Edit: basically I need the redirect to happen to www.url2.com only if www.url.com ends with .php, but not if the .php is followed by variables.
try
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^/site.com/?$ /site.com/cart.php [R=301,NC,L]
Try:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^/?cart.php$ / [L,R=301]
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^stuff.php$ RELATIVE_OR_ABSOLUTE_URL_HERE [L]

.htaccess mod_rewrite won't skip RewriteRule with [S]

As an FYI, I am using the following .htaccess file in located at www.site.com/content/
When a user visits www.site.com/content/login I want it to display the content from www.site.com/content/userlogin.php (masked via rewrite, and not redirect) - which I have done SUCCESSFULLY like so:
RewriteEngine On
RewriteBase /content/
RewriteRule ^login/?$ /content/userlogin.php [NC,L]
However, I would like to add the follwoing: If they try to access www.site.com/content/userlogin.php directly, I want them to get redirected to a 404 page at www.site.com/content/error/404.php
RewriteEngine On
RewriteBase /content/
RewriteRule ^login/?$ /content/userlogin.php [NC,S=1,L]
RewriteRule ^userlogin\.php$ /content/error/404.php [NC,L]
With that in the .htaccess file, both www.site.com/content/login and www.site.com/content/userlogin.php show www.site.com/content/error/404.php
First the S=1 will have no function as the L directive will make any further rewriting stop.
It seems like the first RewriteRule makes Apache go through the .htaccess rules one more time, so you need to know if the first rewrite has happend. You could do this by setting an environment variable like this:
RewriteRule ^login/?$ /content/userlogin.php [E=DONE:true,NC,L]
So when the next redirect occurs the Environment variable actually gets rewritten to REDIRECT_<variable> and you can do a RewriteCond on this one like this:
RewriteCond %{ENV:REDIRECT_DONE} !true
RewriteRule ^userlogin\.php$ /content/error/404.php [NC,L]
Hope this helps
Use %{THE_REQUEST} variable in your code instead:
RewriteEngine On
RewriteBase /content/
RewriteRule ^login/?$ content/userlogin.php [NC,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+userlogin\.php[\s\?] [NC]
RewriteRule ^ content/error/404.php [L]

301 redirect from query string to simple URL

I've had a good look through the first ten pages of search results for "301 redirects" and can't find the answer so here goes...
I've moved from a crappy old CMS that didn't give my pages nice URLs to one that does and I want to set up a 301 redirect for my key pages.
Current URL: http://www.domain.com/?pid=22
New URL: http://www.domain.com/contact/
I'm using Wordpress and my current htaccess file looks like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Any help would be awesome!
Give this a try. All you need to do is check to see if you are on page X and then redirect to page Y. Consider RewriteCond statements to be 'if' statements. If you need more redirects just duplicate the last two lines and edit the paths.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.domain\.com\/?pid=22$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/contact [L,R=301]
You have to check the query string for the value in the "pid" variable and then redirect if the value in that variable matches a page you want to redirect. You can do this with the "RewriteCond" and "RewriteRule" directives like this:
RewriteEngine On
RewriteBase /
# Redirect pid=22 to http://www.domain.com/contact
RewriteCond %{QUERY_STRING} ^pid=22$
RewriteRule ^(.*)$ http://www.domain.com/contact [R=301,L]
You can repeat the "RewriteCond" and "RewriteRule" directives to create additional redirects.

.htaccess: Redirect root url to subdirectory, but keep root URL structure

I have created a site in a subdirectory and would like the site to appear as if it's in the root.
I used the below Mod_Rewrite code to get the site root to redirect to the subdirectory, but I would like the folder the files are held in to not appear.
currently: www.example.com/sitefiles/content/
Would like: www.example.com/content
Thanks
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteRule ^$ /sitefiles/ [L]
RewriteRule (.*) /sitefiles/$1 [L]
If it needs to be done via .htaccess and mod_rewrite, then use this:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/sitefiles/
RewriteRule (.*) /sitefiles/$1 [L]
Since you do not explicitly specify the page when website root will be hit, then there is no need for this line: RewriteRule ^$ /sitefiles/ [L]
You need to add condition for your main rewrite rule to prevent rewrite loop (when already rewritten URL gets rewritten again): RewriteCond %{REQUEST_URI} !^/sitefiles/
Well, the directory should have no reason to appear in any listing, and that mod_rewrite you posted should allow you to do as you said. Still, I would go about it a different way:
Options +FollowSymLinks +Indexes
RewriteEngine on
RewriteBase /
RewriteRule ^$ sitefiles/ [L]
RewriteRule ^(.+)$ sitefiles/$1 [L]
Hope this works.

Resources