apache rewrite rules issue for specific pages - php

I have problem with my .htaccess redirections. When I type:
http://www.domain.com/contact
it goes to the index.php and not the contact.php
here's my .htaccess:
Redirect 301 /clients http://clients.domain.com
RewriteEngine On
SetEnvIf Host ^www\. page=www
SetEnvIf Host ^mob\. page=mobile
RewriteBase /
SetEnvIfNoCase User-Agent "^Wget" bad_bo
#etc ...
Deny from env=bad_bot
RewriteCond %{HTTP_HOST} !^www.domain.com
RewriteRule (.*) http://www.domain.com/$1 [R=301]
RewriteRule ^about/?$ about.php
RewriteRule ^contact/?$ contact.php
rewriterule ^(.*)$ index.php?subdomain=%{ENV:page}&page=$1
in my php i get:
<?php
print_r($_GET);
Array (
[subdomain] => www
[page] => contact.php
)
What am I missing?

Try this rule:
RewriteCond %{HTTP_HOST} !^www.domain.com [NC]
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
RewriteRule ^about/?$ about.php [NC,QSA,L]
RewriteRule ^contact/?$ contact.php [NC,QSA,L]
rewriterule ^([a-z0-9]+)$ index.php?subdomain=%{ENV:page}&page=$1 [NC,QSA,L]
I also added the NC, QSA, L flags to make sure the last rule [L] is executed if match, [NC] for non case and [QSA] for Query String Append.

You need to rewrite your about and contact rules in the following way:
RewriteRule ^about/?$ about.php [L,QSA]
RewriteRule ^contact/?$ contact.php [L,QSA]

Related

redirect to www. without lost variables get

I am redirecting via .htaccess so that when they enter my domain.com site it will take them to www.domain.com. This works fine, but when the url contains some get variable it gets lost.
Example:
Entering domain.com/archive.php?var=something.
I get redirected to www.domain.com/archive.php
Losing the get variable var=something...
In my htaccess I have the following:
RewriteRule (.) http://www.example.com/$1 [R=301,L]
#redirigir index.html e index.php a raiz
RewriteCond %{REQUEST_URI} ^/index.php
RewriteRule ^.$ http://%{HTTP_HOST} [R=301,L]
RewriteCond %{REQUEST_URI} ^/index.html
RewriteRule ^.*$ http://%{HTTP_HOST} [R=301,L]
What am I doing wrong?
Hey i think this can work for you
Options +FollowSymlinks -MultiViews
RewriteEngine on
# for http
RewriteCond %{HTTP_HOST} ^(www\.)?domainA\.com$ [NC]
RewriteCond %{SERVER_PORT} =80
RewriteRule ^(.*)$ http://www.domainB.com/$1 [R=301,L]
# for https
RewriteCond %{HTTP_HOST} ^(www\.)?domainA\.com$ [NC]
RewriteCond %{SERVER_PORT} =443
RewriteRule ^(.*)$ https://www.domainB.com/$1 [R=301,L]

.htaccess change a path to a query strings

I have files in example.com/
contact.php
recent.php
videos.php
watch.php
example.com/page/ > /page/ is not a folder. but i want to work other folders
contact.php (no pagination)
example.com/page/contact
recent.php (pagination > recent.php?page=2)
example.com/page/recent
example.com/page/recent?page=2
watch.php (no pagination > watch.php?title=drama-name)
example.com/page/watch/drama-name
videos.php (pagination with get name > videos.php?name=drama-name&page=2)
example.com/page/videos/drama-name
example.com/page/videos/drama-name?page=2
Here is my full htaccess codes (example.com/.htaccess)
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,NE,L]
RewriteRule ^page/playlist$ /playlist.php [L]
RewriteRule ^page/submit$ /submit.php [L]
RewriteRule ^page/contact$ /contact.php [L]
RewriteRule ^page/search$ /search.php [L]
RewriteRule ^page/recent$ /recent.php [L]
RewriteRule ^page/watch/([^/]+)? /watch.php?title=$1
RewriteRule ^page/videos/([^/]+)? /videos.php?name=$1
But not work in /page/videos/drama-name?page=2 how to fix it? it has another problem?
You can combine regex and shorten your rules in your .htaccess with QSA flag:
RewriteEngine on
RewriteBase /
# add www in host name
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*?)/?$ http://www.%{HTTP_HOST}/$1 [R=301,NE,L]
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [NE,R=301,L]
RewriteRule ^page/(submit|contact|search|recent|playlist)/?$ $1.php [L,NC]
RewriteRule ^page/(videos|watch)/([^/]+)/?$ $1.php?title=$2 [L,QSA,NC]

htaccess get other variables

This is my whole .htaccess code:
RewriteEngine on
Options +FollowSymlinks
Header set X-UA-Compatible "IE=Edge,chrome=1"
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,NC,L]
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
RewriteRule admin/^([^\.]+)$ ./admin.php?get=$1 [L,QSA]
RewriteRule admin/ ./admin.php [L,QSA]
php_value date.timezone "Europe/Bratislava"
I want that, when users are at http://something/admin/ it will show up admin.php - that is this row RewriteRule admin/ ./admin.php [L,QSA] and it works well.
But when users are at http://something/admin/profile/email/ it should send something like admin.php?get=profile/email/, but it doesn't. What is wrong?

Conditional Redirect with htaccess

I have a site: blog.example.com.
I want blog.example.com to go to example.com/news
But I want blog.example.com/tag/mytag or blog.example.com/category/mycategory to go to example.com/tag/mytag or example.com/category/mycategory
So far I have my .htaccess like this but of course it doesnt work:
RewriteEngine on
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteRule ^(.*)$ http://www.example.com/news/ [R=301,L]
You can use this in root .htaccess of blog.example.com:
RewriteEngine on
RewriteRule ^/?$ http://www.example.com/news [R=301,L]
RewriteRule ^(.+)$ http://www.example.com/$1 [R=301,L,NE]

Redirect 301 an address with a / in .htaccess

I want to redirect an address with a / :
mywebsite.com/img-41/
to another without the /:
mywebsite.com/img-41
Of course the 41 is a variable.
I tried this:
RewriteRule ^img-([0-9]*)/$ /img-$1 [L,R=301,NC]
But it doesn't work :(
Could you please help me?
Thanks in advance, lucas.
EDIT:
SetEnv PHP_VER 5_TEST
SetEnv REGISTER_GLOBALS 0
Options +FollowSymlinks
# Activation du module de réécriture d'URL :
RewriteEngine on
RewriteRule ^img-([0-9]*)$ img.php?&numlsv=$1 [L]
RewriteRule ^img-([0-9]*)/$ img.php?&numlsv=$1 [L]
RewriteRule ^home-([0-9]*)-([a-zA-Z0-9]*)$ index.php?&numpage=$1&auteur=$2 [L]
RewriteRule ^home-([0-9]*)$ index.php?&numpage=$1 [L]
RewriteRule ^img-([0-9]*)/$ /img-$1 [L,R=301,NC]
Rewritecond %{HTTP_HOST} ^www.mywebsite.com$
Rewriterule ^(.*) http://mywebsite.com/$1 [QSA,L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ / [R=301,L]
SetEnv SESSION_USE_TRANS_SID 0
This is the entire .htaccess.
When I say that it doesn't work, I mean that when I go to my address with the slash, the navigator stays in this address, it doesn't go to the address without the slash :(
Try adding a rewrite base like this
RewriteBase /
RewriteRule ^img-([0-9]+)/$ img-$1 [L,R=301,NC]
Your rewrite rule only redirects requests that start with img
Try adding a / like this:
RewriteRule ^/img-([0-9]*)/$ /img-$1 [L,R=301,NC]
I replied before your edit,
You have already performed an internal redirect with an L flag before this line
Options +FollowSymlinks
# Activation du module de réécriture d'URL :
RewriteEngine on
RewriteRule ^img-([0-9]*)/$ /img-$1 [L,R=301,NC]
RewriteRule ^img-([0-9]*)/$ img.php?&numlsv=$1 [L]
RewriteRule ^home-([0-9]*)-([a-zA-Z0-9]*)$ index.php?&numpage=$1&auteur=$2 [L]
RewriteRule ^home-([0-9]*)$ index.php?&numpage=$1 [L]
Rewritecond %{HTTP_HOST} ^www.mywebsite.com$
Rewriterule ^(.*) http://mywebsite.com/$1 [QSA,L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ / [R=301,L]
SetEnv SESSION_USE_TRANS_SID 0
Order matters when it comes to rules, you have rules that map the pattern you're trying to match before the rule that does the redirect. Therefore, the redirect never happens, the URI has already been changed!
RewriteRule ^img-([0-9]*)$ img.php?&numlsv=$1 [L]
RewriteRule ^img-([0-9]*)/$ img.php?&numlsv=$1 [L]
RewriteRule ^home-([0-9]*)-([a-zA-Z0-9]*)$ index.php?&numpage=$1&auteur=$2 [L]
RewriteRule ^home-([0-9]*)$ index.php?&numpage=$1 [L]
RewriteRule ^img-([0-9]*)/$ /img-$1 [L,R=301,NC]
Here, the 2nd rule:
RewriteRule ^img-([0-9]*)/$ img.php?&numlsv=$1 [L]
has the exact same pattern as your last rule:
RewriteRule ^img-([0-9]*)/$ /img-$1 [L,R=301,NC]
Thus, the first rule matches it and the last rule will never get applied. You always want your redirects first:
Options +FollowSymlinks
# Activation du module de réécriture d'URL :
RewriteEngine on
RewriteRule ^img-([0-9]*)/$ /img-$1 [L,R=301,NC]
Rewritecond %{HTTP_HOST} ^www.mywebsite.com$
Rewriterule ^(.*) http://mywebsite.com/$1 [QSA,L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ / [R=301,L]
RewriteRule ^img-([0-9]*)$ img.php?&numlsv=$1 [L]
RewriteRule ^img-([0-9]*)/$ img.php?&numlsv=$1 [L]
RewriteRule ^home-([0-9]*)-([a-zA-Z0-9]*)$ index.php?&numpage=$1&auteur=$2 [L]
RewriteRule ^home-([0-9]*)$ index.php?&numpage=$1 [L]

Categories