htaccess regex rewrite - regex

I'm trying to change this:
/news/index.php?id=101&title=this-is-a-very-long-title
to this:
/news/this-is-a-very-long-title/
with htaccess rewrite rule. Any idea how?
I have tried:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^.+/news/index.php?title=$1 [NC,L,R]

I think this is what you need:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !index\.php [NC]
RewriteRule ^.*/([^/]+)/? news/index.php?id=101&title=$1 [L,NC]
Maps silently
http://example.com/news/this-is-a-very-long-title/ with or without trailing slash
To:
http://example.com/news/index.php?id=101&title=this-is-a-very-long-title
In your rule the string id=101& is not present in the substitution URL, so remove it from the rewrite rule if it is not needed.
For permanent and visible redirection, replace [L,NC] with [R=301,L,NC].

Related

RewriteRule with optional parameter for redirection in .htacces

I would like to rewrite from:
www.domain.de/123/hello
to:
www.domain.de/index.php?a=$1&b=$2
I have this rule:
RewriteEngine On
Options +FollowSymlinks
RewriteBase /
RewriteRule ^([^/]+)/([^/]+)?$ index.php?a=$1&b=$2 [L,QSA,NC]
it works fine. But I also would like that it also works with the input:
www.domain.de/123
(Here I will get an 404 error.)
The &b shout be empty in this case.
How to achive this?
You may use these rules with an optional match for 2nd paramter:
RewriteEngine On
Options +FollowSymlinks
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)(?:/([^/]+))?/?$ index.php?a=$1&b=$2 [L,QSA]
Here (?:/([^/]+))? is an optional match to make sure above rule matches both:
www.domain.de/123/hello
and
www.domain.de/123

htaccess both ending slash and no slash to extension(.html)

I have the following content in .htaccess
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(/|\..+)$
RewriteRule ^ %{REQUEST_URI}.html [L,NC,R=301]
What I'm trying to do is to redirect site.com/category and site.com/category/ to site.com/category.html
The code that I have so far is doing redirecting /category to /category.html but not /category/(Multiple choices shows up)
I would like some explanation of the regex in this line as well:
RewriteCond %{REQUEST_URI} !(/|\..+)$
You can make trailing slash optional:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /category/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/.]+)/?$ $1.html [L,R=301]
This condition:
RewriteCond %{REQUEST_URI} !(/|\..+)$
is matching request URI if doesn't contain a / or a DOT plus some text.
However as you can see this is not even needed as per my suggested answer.

HTACCESS Dealing with 2 RewriteRules

RewriteEngine On
Options +FollowSymLinks
RewriteBase /
RewriteRule ^api/people/([^/]*)$ api/people.php?id=$1
RewriteRule ^api/people/([^/]*)/([^/]*)$ api/people.php?action=$1&id=$2
In the above example, there are to RewriteRules one detects if a url is for example domain.com/api/people/1 and the other detects domain.com/api/people/settings/1
I usually get Error 500 when I try access the latter url, is there a way to sort out the HTACCESS so that it can detect if the url is either the first or last?
Keep your .htaccess like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^api/people/([^/]+)/?$ api/people.php?id=$1 [L,QSA]
RewriteRule ^api/people/([^/]+)/([^/]+)/?$ api/people.php?action=$1&id=$2 [L,QSA]

.htaccess not working only with internet explorer

i have site, that has to rewrite site.ru and www.site.ru to www,site.ru/ru_RU.
I can't access any Apache config files. In htaccess:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^site.ru$
RewriteRule (.*) http://www.site.ru/ru_RU [QSA]
RewriteCond %{HTTP_HOST} ^www.site.ru$
RewriteRule (.*) http://www.site.ru/ru_RU [QSA]
RewriteCond %{REQUEST_URI} ^/news
RewriteRule (.*) /news [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [QSA]
</IfModule>
It's working in firefox or chrome, but in IE i get "this page can't be displayed". Tested on IE10 and IE8 (not compatibility view) on few computers.
If i write some junk in .htacess, i get 500 error in IE. Without .htaccess site loads ok, but i need it to rewrite url. Any ideas how to fix it?
Your flags are all wrong. Modify your rules to this:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^site\.ru$ [NC]
RewriteRule ^ http://www.site.ru/ru_RU [L,R]
RewriteCond %{HTTP_HOST} ^www\.site\.ru$ [NC]
RewriteRule !ru_RU /ru_RU [NC,L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [L,QSA]
</IfModule>
Reference: Apache mod_rewrite Introduction
I can't test it now, but here is my guess:
I think http_host ONLY contains the host part, not the uri. So you end up with an endless loop as www.site.ru is will always match again after your rewrite.
You will need another rule that checks if the uri is empty. Like this (untested):
RewriteCond %{REQUEST_URI} ^$
RewriteRule (.*) /ru_RU [QSA]
You might need another condition for the case that the uri contains a slash.

Htaccess - replace .html with a forward slash

I want to redirect URLs of this form:
/page.html?variable=value&othervar=true&thirdvar=100
To this:
/page/?variable=value&othervar=true&thirdvar=100
So basically I just want to replace the .html in the middle of the URL with a forward slash, but I need to preserve the get string that comes with it. This is what I tried:
RewriteRule ^page.html(.+)$ /page/$1 [L,R=301]
But this doesn't appear to be working for me. I've made similar things work recently but I can't figure out what I'm missing here. Thanks for any input.
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 /
# external redirect from /example.html to /example
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+([^.]+)\.html [NC]
RewriteRule ^ /%1/ [R=301,L]
# internal forward from /example/ to //example.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(.+?)/?$ /$1.html [L]