HTACCESS Dealing with 2 RewriteRules - regex

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]

Related

mod_rewrite() too many internal redirects

I have successfully implemented the basic facebook like URLs:
http://example.com/(username) which internally calls http://example.com/sites/(username)
using the following mod_rewrite code:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/sites/
RewriteRule ^([^/]+)/?(.*)$ sites/$1/$2 [NC,L]
Please note that the (username) folder exists inside the sites folder
The above works perfectly fine. But now i want to prettify my other urls like:
http://example.com/(username)/Image/2/ this should internally call http://example.com/sites/(username)/index.php?type=image&id=2
To do this i added the following code in the second last line:
RewriteRule ^([^/]+)/Image/(.*)$ /sites/$1/index.php?type=image&id=$2 [NC,L]
I was hoping the [L] at the end will stop the rule from being computed after this. But from the logs it turns out it goes into an infinite loop and is truncated.
Can someone point out what i'm doing wrong here?
Inside your DocumentRoot folder you can have this .htaccess:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([^/]+)/(\d+)/?$ sites/$1/index.php?type=$2*id=$3 [QSA,L]
RewriteRule ^((?!sites[^/]+)(/.*)?$ sites/$1$2 [NC,L]

Mod_Rewrite Htaccess Internal Redirect Just Not Working

I have tried so many things in the past hour and nothing works.
I have a website called example.com
I have this code:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+).html$ index.php?p=$1&i=$2 [N]
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?p=$1
When I go on example/forums/discussion-f3.html it works.
When I try /forums/introductions-f2.html, it redirects me to /index.php/?p=forums&i=introductions-f2. Also, http://craftshaft.org/forums/ sends me to http://craftshaft.org/index.php/?p=forums but when I do it without the slash and remove it from the code, it works, but I need the slash at the end so it looks like a folder.
Basically, I'd like to be able to view URLs like this:
/forums.html (to /index.php?p=forums)
forums/thread-name.html (to
index.php?p=forums&i=thread-name)
Place this code in root .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
RewriteRule ^([\w-]+)(\.html)/?$ index.php?p=$1 [L,NC,QSA]
RewriteRule ^([\w-]+)/([\w-]+)\.html$ index.php?p=$1&i=$2 [L,NC,QSA]

Apache non www to www badly re writting arrays on URL

we have the following .htaccess configuration:
RewriteEngine on
allow from all
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
# otherwise forward it to index.php
RewriteRule . index.php
RewriteCond %{HTTP_HOST} ^domain1\.domain2\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain1\.domain2\.com$
RewriteRule ^(.*)$ "http\:\/\/www\.domain1\.com\/$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^domain1.com$
RewriteRule ^/?$ "http\:\/\/www\.domain1\.com\/" [R=301,L]
Between other things we want to redirect all non-www URLs to the ones with the www.
Everything works fine, except for the pages where the URL is like this:
index?Form%5bplace%5d=Caribbean&Form%5bdestination%5d=Virgin+Islands&Form%5btype%5d=A
When we enter the URL without the www our redirect ends up with the following URL:
index?Form%255bplace%255d=Caribbean&Form%255bdestination%255d=Virgin+Islands&Form%255btype%255d=A
Which gives an 404 error because it is not recognized.
Any idea how to avoid this?
Replace your code with this:
allow from all
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# if a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
# otherwise forward it to index.php
RewriteRule . index.php [L]
RewriteCond %{HTTP_HOST} ^domain1\.domain2\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain1\.domain2\.com$
RewriteRule ^ http://www.domain1.com%{REQUEST_URI} [NE,R=301,L]
RewriteCond %{HTTP_HOST} ^domain1\.com$
RewriteRule ^/?$ http://www.domain1.com/ [R=301,L]
What's happening is that the % symbol is getting escaped to %25.
You can avoid this using the NE flag on your rules

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]

htaccess regex rewrite

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