htaccess rewrite if no rewrite was provided - .htaccess

First off I'm not an expert in htaccess stuff, but i try to accomplish the following for some SEO fixes.
When the url is loaded, without any params/rewrite it should get some data attached before it continues.
For example:
http://www.domain.com >>> http://www.domain.com/en/
I thought the rewrite was right like this, but didn't work (500)
RewriteCond %{REQUEST_URI} !^(en|nl|de|etc)$
RewriteRule ^(.*)$ /en/ [L,R=301]
added
RewriteRule ^(/?[^/]+) /index.php?rewrite=1 [L] # tell php we got a rewrite

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)$ $1/en/ [NC]

Related

Weird behavior with .htaccess

I'm new in rewrite rule and I encounter a little issue with my .htaccess
RewriteEngine On
RewriteRule ^toto\.html$ /toto.php [NC,L]
RewriteCond %{REQUEST_URI} !^/(my|your)/template\.html [NC]
RewriteCond %{REQUEST_URI} !^/(my|your)/example\.html [NC]
RewriteRule ^([^/]*)/([^/]*)/([^\.]*)\.html$ /$1.php?type=$2&name=$3 [NC,L]
RewriteRule ^([^/]*)/([^\.]*)\.html$ /index.php?type=$1&name=$2 [L]
RedirectMatch 404 \.(htaccess|htpasswd|ini|log|sh|inc|bak|bkp|sql|json)$
I tested it on https://htaccess.madewithlove.be/ which always give me the right rewrite.
When I test it on my webhosting :
I go to http://mydomain.ovh/my/object.html
The rewrite is right : http://mydomain.ovh/index.php?type=my&name=object
But when I go to
http://mydomain.ovh/write/my/object.html
http://mydomain.ovh/extract/my/object.html
http://mydomain.ovh/fill/your/object.html
http://mydomain.ovh/write/your/data.html
http://mydomain.ovh/extract/your/data.html
The rewrite is wrong : The requested URL /redirect:.php was not found on this server.
I don't understand this behavior...
So I tested new rewrite rules making a common script for write/fill action :
RewriteEngine On
RewriteRule ^toto\.html$ /toto.php [NC,L]
RewriteCond %{REQUEST_URI} ^/extract/(my|your)/[^\.]+\.html$ [NC]
RewriteRule ^([^/]*)/([^/]*)/([^\.]*)\.html$ /extract.php?type=$2&name=$3 [L]
RewriteCond %{REQUEST_URI} !^/extract.* [NC]
RewriteCond %{REQUEST_URI} !^/(my|your)/template\.html [NC]
RewriteCond %{REQUEST_URI} !^/(my|your)/example\.html [NC]
RewriteRule ^([^/]*)/([^/]*)/([^\.]*)\.html$ /set.php?type=$2&name=$3 [NC,L]
RewriteRule ^([^/]*)/([^\.]*)\.html$ /index.php?type=$1&name=$2 [L]
RedirectMatch 404 \.(htaccess|htpasswd|ini|log|sh|inc|bak|bkp|sql|json)$
I tested it on https://htaccess.madewithlove.be/ which always give me the right rewrite.
And when I test it on my webhosting :
I go to http://mydomain.ovh/my/object.html
The rewrite is right : http://mydomain.ovh/index.php?type=my&name=object
I go to http://mydomain.ovh/fill/my/object.html or http://mydomain.ovh/write/your/data.html
The rewrite is right : http://mydomain.ovh/set.php?type=my&name=object or http://mydomain.ovh/set.php?type=your&name=data
But when I go to http://mydomain.ovh/extract/my/object.html or http://mydomain.ovh/extract/your/data.html
The rewrite is always wrong : the request reach the script but without query string... (and it appear to be a redirect?)
["PATH_TRANSLATED"]=>
string(19) "redirect:/index.php"
["PATH_INFO"]=>
string(30) "/my/object.html"
["SCRIPT_NAME"]=>
string(11) "/extract.php"
["REQUEST_URI"]=>
string(37) "/extract/my/object.html"
["QUERY_STRING"]=>
string(0) ""
["PHP_SELF"]=>
string(41) "/extract.php/my/object.html"
Can someone help with these rewrite rules ?
You need to turn off option MultiViews at top of your .htaccess which it seems is turned on for your website in Apache config.
Add this at top of your .htaccess:
Options -MultiViews
Option MultiViews (see http://httpd.apache.org/docs/2.4/content-negotiation.html) is used by Apache's content negotiation module that runs before mod_rewrite and makes Apache server match extensions of files. So if /file is the URL then Apache will serve /file.html.
Once you make this change, test in a new browser to avoid old browser cache.

.htaccess rewrite rule with get params

I need to be able to rewrite http://example.com/staging/details/?postid=23 so that it becomes http://example.com/staging/grandhotelterduin
I am not sure how the .htaccess rule should be?
I have came up with something like this
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^grandhotelterduin/(.*)$ ./details/?postid=23 [L,NC]
I am not sure I am doing this correctly, can you plz help
I want it such that the http://example.com/staging/grandhotelterduin/ get redirected to http://example.com/staging/details/?postid=23 but the url that appears in the browser is `http://example.com/staging/grandhotelterduin
Use this:
RewriteEngine On
RewriteRule ^grandhotelterduin$ /staging/details/?postid=23 [L]
It will give you the following URL:
http://sheetz.nl/grandhotelterduin
EDIT:
RewriteCond %{THE_REQUEST} /staging/details/?postid=([0-9]+) [NC]
RewriteRule ^ /staging/grandhotelterduin [L,R]

.htaccess rewriting GET

I've never needed to use a .htaccess before and I'm fairly new to coding I'm trying to get localhost/index.php?id=123456789 to be passed as localhost/123456789/ but I just cant get the HTaccess right, i've tried everything I could find from prevoius posts here, haha!
My current HTACCESS looks like this, however it doesnt do what I want.
RewriteEngine On
RewriteRule ^id/(\d+)$ /index.php?id=$1 [NC,QSA,L]
You can do it with mod_rewrite in .htaccess however I'm not sure from your question which direction you want it to be passed to.
If you want people to type the php script in the URL bar you want:
RewriteEngine on
RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule ^index.php$ %1/
Or if you want people to enter the "pretty" version but for your server to load the PHP script you need:
RewriteEngine on
RewriteRule ^([0-9]+)/$ index.php?id=$1&%{QUERY_STRING}
For both ways and 301 redirect to pretty URL:
RewriteEngine on
RewriteRule ^([0-9]+)/$ index.php?id=$1&%{QUERY_STRING} [L]
RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule ^index.php$ %1/ [L,R=301]

.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]

Redirect all pages except one page to sub-domain htaccess

I have a index.php in my main domain root
domain.com/index.php
And ive moved my forums which was in the same "root" to a subdomain
forums.domain.com
I need to Redirect everything except the index.php
ive tryed loads of ways and none seem to work
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !index\.php$
RewriteRule ^(.*)$ http://forums.domain.com [L,R]
RewriteEngine On
RewriteCond %{HTTP_HOST} animelon\.com [NC]
RewriteCond %{REQUEST_URI} !^index\.php$
RewriteRule ^(.*)$ http://forums.domain.com/$1 [R=301,L]
If anyone has any ideas that would be great
as for the above codes I would them googling about.
Cheers
You may use RedirectMatch instead of rewriting, that is, replace all the rewrite block you are showing with:
RedirectMatch ^(/(?!index\.php).*) http://forums.domain.com$1
You can see the full explanation of the regex on Regexr here. In brief, it sends all the URIs NOT beginning with /index.php to forums.domain.com.
If you don't need any other rewrite rule, you can turn off rewriting by removing all the lines beginning with "Rewrite" from your .htaccess.

Resources