Mod Rewrite problem - apache

.htacces
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9_-]+)$ sinj.com.hr/index.php?var1=$1 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ sinj.com.hr/index.php?var1=$1&var2=$2 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ sinj.com.hr/index.php?var1=$1&var2=$2&var3=$3 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ sinj.com.hr/index.php?var1=$1&var2=$2&var3=$3&var4=$4 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/$ sinj.com.hr/$1 [R=301,L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ sinj.com.hr/$1/$2 [R=301,L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ sinj.com.hr/$1/$2/$3 [R=301,L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ sinj.com.hr/$1/$2/$3/$4 [R=301,L]
There is folder sinj.com.hr/administracija and when I try to access http://localhost/sinj.com.hr/administracija I am redirected to http://localhost/sinj.com.hr/administracija?var1=administracija
What I would like is when user enters http://localhost/sinj.com.hr/administracija that he is redirected to http://localhost/sinj.com.hr/administracija/index.php. I tried to do this with header("Location:... ") but it always redirects me to http://localhost/sinj.com.hr/administracija?var1=administracija. If folder administracija is renamed then header() function works. Any ideas how to solve this?
Thanks,
Ile

Try this rule to test if the request can be mapped to a directory that contains an index.php file:
RewriteCond %{REQUEST_FILENAME}/index.php -f
RewriteRule ^ %{REQUEST_URI}/index.php
Additionally you can use this single rule to redirect any requests with a URL path that ends with a slash the one without:
RewriteRule ^(.+)/$ sinj.com.hr/$1 [R=301,L]

Related

redirect any subdomain to main domain with .htaccess

I'm attempting to redirect any direct attempts to access sub.example.com/login over to the original domain/uri at example.com/login, and from the number of questions I've already read in regards to this exact thing, it would appear easy on the surface of it...
I don't know if there's something going on that's overriding the desired outcome, but I have the following rules in my .htaccess file, yet I'm still able to access sub.example.com/login without being redirected.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com/login$ [NC]
RewriteRule ^(.*)$ http://example.com/login$1 [L,R=301]
Actually, just for complete clarity, here's my entire .htaccess file contents. Maybe somebody else can see something wrong that I'm missing.
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
# add a trailing slash to /wp-admin
RewriteRule ^wp-admin$ wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^(.*\.php)$ $1 [L]
RewriteRule . index.php [L]
# redirect subdomain login attempts to main domain
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com/login$ [NC]
RewriteRule ^(.*)$ http://example.com/login$1 [L,R=301]
Put Redirect subdomain login rewrite condition & rule just after RewriteBase line. That should help to address your issue.
RewriteEngine On
RewriteBase /
# Redirect all subdomains to example.com
RewriteCond %{HTTP_HOST} !^(.*)\.example\.com$ [NC]
# Redirect only bar & foo subdomains to example.com
#RewriteCond %{HTTP_HOST} !^(bar|foo)\.example\.com$ [NC]
RewriteRule ^ http://example.com/ [L,R]

Silent rewrite using .htaccess

I'm trying to do the following on my website:
Redirect / to /home
Redirect /index.php to /home
The directory home actually doesn't exist, but should be linked to the index.php file. So the server will run index.php, while the client sees server/home as location.
This should be done by a silent request, I believe, but how?
What I have:
RewriteEngine On
RewriteRule ^(.*[^/])$ /$1/ [L,R=301] # for trailing slash
RewriteRule ^home/$ http://server/index.php [NC]
RewriteRule ^/$ http://server/home [L,NC]
RewriteRule ^index.php/$ http://server/home [L,NC]
Not working though, it infinitely redirects... how should I do this?
RewriteEngine On
RewriteBase /
#RewriteRule ^(.*[^/])$ /$1/ [L,R=301] # for trailing slash
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule ^(/|index\.php/?|home)$ /home/ [L,R=302]
RewriteRule ^home/$ /index.php [NC]

HTACCESS - Applying some simple mod_rewrite rules

this is urgent a lot. I need help with some htaccess rules. The clue is when user receives an answer for sth the real url will be www.mydomain.com/results.html/sth but he must can see in his browser only www.mydomain.com/sth only and when i redirect him to home page (www.mydomain.com/index.html) the url has to be www.mydomain.com only.
Thanks
#this is for the www.yourdomain.com/index.html
RewriteRule ^$ index.html [L]
RewriteRule ^/$ index.html [L]
#this is for the results
RewriteRule ^([^/]+)$ results.html?$1
RewriteRule ^([^/]+)/$ results.html?$1
Replace the first 3 lines of code with:
DirectoryIndex index.html
it should have worked by default though
This should work:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(results|index)\.html
RewriteCond %{REQUEST_URI} !^/?$
RewriteRule ^(.*)$ /results.html/$1
RewriteRule ^index\.html$ / [R=301,L]

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.

.htaccess redirect 301 using rewrite rule

I'm trying to beautify some urls. I configured the htaccess file so my urls are changed:
old url: http://mysite.com/index.php?id=45tye4
new url: http://mysite.com/45tye4
I want now to permanently redirect old urls to new urls. This is what I try with no luck:
RewriteRule ^index.php?id=(.*)$ $1 [R=301,L]
The main problem seems to be the '?' in the url. When I try the same url without ? the redirect works. I also tried other variants with no luck:
RewriteRule ^index.php\?id=(.*)$ $1 [R=301,L]
RewriteRule ^index.php[\?]id=(.*)$ $1 [R=301,L]
Update:
I added the redirection according to anubhava instructions. The redirection works, but unfortunately I get into a redirect loop. I thought [L] flag should solve the redirection loop, but it doesn't.
RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule ^index\.php/?$ /%1? [R=301,L]
RewriteRule ^(.*)$ index.php?id=$1 [L]
RewriteRule matches only REQUEST_URI. You have to use RewriteCond to match Query string
Try this code:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteCond %{THE_REQUEST} ^GET\s/+index\.php [NC]
RewriteCond %{QUERY_STRING} (^|&|\?)id=(.*)(&|$) [NC]
RewriteRule . /%2? [R=301,L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?id=$1 [L]
This will redirect old URI of /index.php?id=45tye4 to new URI: /45tye4