Stop Apache mod_rewrite affecting Redirect rules - apache

I have the following .htaccess file:
Redirect 301 /test/example-1 /test/example-2
RewriteEngine On
RewriteBase /test/
RewriteRule ^(.*)$ index.php?_url=$1 [QSA,L]
I want the redirect to happen without mod_rewrite interfering with it, but at the moment when you hit /test/example-1 it redirects to /test/example-2?_url=example-1.
Is there any way to stop the query parameter from the rewrite rule getting append to the redirect URL? Thanks.

Have your rewrite rule as:
RewriteEngine On
RewriteBase /test/
RewriteCond %{REQUEST_URI} !/(example-1|index\.php) [NC]
RewriteRule ^(.*)$ index.php?_url=$1 [QSA,L]
But in general you shouldn't mix mod_alias rules with mod_rewrite rules. Your code can be rewritten as:
RewriteEngine On
RewriteBase /test/
RewriteCond %{THE_REQUEST} /example-1[\s?/] [NC]
RewriteRule ^ example-2 [R=301,L]
RewriteRule ^(.*)$ index.php?_url=$1 [QSA,L]

Related

the right way redirect url using .htaccess

what is the right way to redirect page using .htaccess?
for ex i want to redirect myweb.com/dorama/901110204/todome-no-kiss--paralel to myweb.com/dorama/1101071510/todome-no-kiss--parallel
but when i try to access old link, link direct to myweb.com/dorama/1101071510/todome-no-kiss--parallel?cat=dorama&idp=901110204&post=todome-no-kiss--paralel
my full .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
#Redirect Dead Link
Redirect 301 /dorama/901110204/todome-no-kiss--paralel http://www.piratefiles.org/dorama/1101071510/todome-no-kiss--parallel
Redirect 301 /dorama/601181905/life-as-a-girl http://www.piratefiles.org/dorama/1101072233/life-as-a-girl
Redirect 301 /dorama/801130021/todome-no-kiss http://www.piratefiles.org/dorama/1101071405/todome-no-kiss
RewriteRule ^([A-Za-z]+)/([0-9]+)/([\w-]+)/?$ view.php?cat=$1&idp=$2&post=$3&%{QUERY_STRING} [NC,L]
RewriteRule ^category/([\w-]+)/?$ category.php?cat=$1&%{QUERY_STRING} [NC,L]
# To externally redirect /dir/abc.php to /dir/abc
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [QSA,NC,L]
</IfModule>
ErrorDocument 404 http://www.piratefiles.org/404.php
RewriteRule directives "run" before Redirect ones do. Thus, if you want to force the redirections to run in a particular order, you need to convert them all to Rewrite directives.
Replace the three Redirect directives with:
RewriteRule ^dorama/(\d+)/(todome-no-kiss--paralel|life-as-a-girl|todome-no-kiss)$ http://www.piratefiles.org/$1/$2 [R=301]
Tweak as necessary, but that's the reason it's failing, and a proposed solution.
(Also, standard note here: If you have access to your main server config, .htaccess files should be avoided whenever possible. .htaccess files are for people that don't have access to the main config.)

htaccess redirection - stop redirecting subdomain

I have this code in my .htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
RedirectMatch 301 ^/de/(.*)$ http://de.example.com/$1
My problem is that it redirects even http://xyz.example.com/de/ to http://de.example.com/
What I need is still redirecting from example.com/de to de.example.com
but without redirecting xyz.example.com/de to de.example.com
Could you help me to modify that?
You ar mixing directives from 2 different Apache modules mod_rewrite and mod_alias. Stick to mod_rewrite only and have this rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^de/(.*)$ http://de.example.com/$1 [L,NC,R=302]

htaccess simple rewrite rule doesn't work

I've never did rewrite rules and everything I try out and find at google doesn't work. Especially creating multiple rewrite rules is hard for me because I don't know how the right syntax is and how a proper implementation looks like (1 Rewrite Condition or multiple and similar questions).
Thus I would be happy to get a result for my following try:
https://www.domain.com/our_accounts.php -> https://www.domain.com/accounts
The http -> https rule is already working. Maybe there is also the problem with my rewrite rules because I may need to add them before my https rules ?? I hope you guys can help me with this.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
# First rewrite to HTTPS:
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Now, rewrite any request to the wrong domain to use www.
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Rules for readable URLs
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
RewriteRule ^accounts$ /our_accounts.php [L]
</IfModule>
Try your htacess file this way. I also condensed the HTTP and www to one rewrite rule. Just replace yoursite.com with your real site.
<IfModule mod_rewrite.c>
RewriteEngine On
# First rewrite to HTTPS and redirect www:
RewriteCond %{HTTPS} !^on [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.yoursite.com/$1 [L,R=301]
RewriteRule ^our_accounts.php$ /accounts [R=301,L]
# Rules for readable URLs
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
</IfModule>

Two level url rewrite with mod_rewrite

I need something like this index.php?page=home to be /home
and
index.php?page=products&cat=chairs to be /products/chairs
What would be the mod_rewrite rule to achieve this?
Edited
I have other pages than home, so my full content of .htaccess file is now
RewriteEngine On
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]
RewriteRule ^/products(:/([^/]+))? /index.php?page=products&cat=$1 [L]
But when I navigate to /products/chairs I get a "Object not found" error.
RewriteEngine on
RewriteRule ^/home/?$ /index.php?page=home [L]
RewriteRule ^/products(:/([^/]+))? /index.php?page=products&cat=$1 [L]
It rewrites both /home and /home/, both /products and /products/ (you didnt specify which was your intended behavior); beware that you will get an empty cat parameter in those cases.
EDIT: this is a tested .htaccess, with R=301 left on it to see rewriting result, with both .htaccess and index.php in same directory (webroot in my case):
RewriteEngine on
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [R=301,L]
RewriteRule ^products/([^/]+)?/? index.php?page=products&cat=$1 [R=301,L]
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?page=(products)&cat=([^\s]+) [NC]
RewriteRule ^ %1/%2 [R=302,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?page=(home)\s [NC]
RewriteRule ^ %1 [R=302,L]
RewriteRule ^(products)/([^/]+)/?$ index.php?page=(products)&cat=$1 [L,QSA,NC]
RewriteRule ^(home)/?$ index.php?page=$1 [L,QSA,NC]
Change R=302 to R=301 after you verify it's working fine for you.

How can I use mod_rewrite to 301 redirect example.com to www.example.com?

I need to redirect any URLs without "www." to URLs with "www." for better search engine optimization. I read that this is possible with mod_rewrite and .htaccess files, but I do not know the right code to use. Can anyone help?
Create a file called .htaccess in your root folder (the one where, say, index.html or index.php resides). Put the following into it:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yourdomain\.com [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [L,R=301]
There is an excellent example of this in Apache's URL Rewriting Guide.
The following code would redirect any non-www request to a www request:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://www.example.com/$1 [L,R=301]
You'd want to put this inside the <Directory> directive of your .htaccess file, or apache config file.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) http://www.example.com/$1 [L,R=301]
To remove www from your url website use this code on .htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
To force www in your website url use this code on .htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^YourSite.com$
RewriteRule ^(.*)$ http://www.yourSite.com/$1 [R=301]
RewriteCond %{REQUEST_fileNAME} !-d
RewriteCond %{REQUEST_fileNAME} !-f
RewriteRule ^(([^/]+/)*[^./]+)$ /$1.html [R=301,L]
Were "YourSite.com" you must replace for your url.
In addition to using mob_rewrite you can do this with a virtual host directive
<VirtualHost example.com>
ServerName example.com
Redirect permanent / http://www.example.com
</VirtualHost>
I usually do it the other way around to remove the extraneous 'www'.