Configuring mod_rewrite issue - regex

I have in my htaccess the following:
DirectoryIndex index.py
RewriteEngine On
RewriteBase /
RewriteRule ^([0-9]+)$ index.py?id=$1 [L]
RewriteCond %{THE_REQUEST} /index\.py [NC]
RewriteRule ^(.*?)index\.py$ /$1 [L,R=302,NC,NE]
First rule sends someone looking for
mydomain.com/<digits>
to mydomain.com/index.php?id=, second rule fixes hrefs inside index.py.
But when I add
RewriteBase /
RewriteRule ^([^0-9]+)$ index.py [L]
To make anyone whose request is not only digits go to index.py, divs in index.py just kinda disappear. What is the problem?

Probably because you're rewriting everything to index.py, including your style sheets or scripts or whatever. Try changing the rule to:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^0-9]+)$ index.py [L]

Related

Htaccess Public Folder Rewrite Rule

I'm working on a little custom MVC project in PHP and am having some issues with the htaccess. Originally, my htaccess was routing all traffic to index.php in my root dir, and passing the remaining path as an argument. This was working perfectly with this code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?path=$1 [NC,L,QSA]
The problem I'm having now is that I need to move the index.php file into a /public directory. I scoured the internet for answers, and found a code snippet that kinda works in that it seems to get there as long as it is just hitting /, but as soon as the url becomes /register/ or anything else it just 404's.
# Get rid of /public/ in the URL, and route all requests through
# the Index.php file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /public/index.php?path=$1 [NC,L,QSA]
RewriteCond %{REQUEST_URI} !/public/ [NC]
RewriteRule ^(.*?)/?$ public/$1 [L]
I know that last line makes no sense in when there is the rewrite to index.php with ?path that seems proper to me (at least it passes the argument like I want!) but without both these lines it doesn't seem to work, and I've been trial-and-erroring this for hours. Hopefully someone can help out! Cheers!
Keep only this content in your .htaccess:
DirectoryIndex index.php
RewriteEngine On
RewriteRule ^/?$ public/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ public/?path=$1 [L,QSA]

remove folder from url using .htaccess

I know there are plenty of other questions similar to this one, i tried followinf some of those, but with no luck.
I have a website called test.com and i need, when someone seraches for test.com to show the content in test.com/home, without having home in the uerl.
My htaccess file at the moment looks like this:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule /(.*) home/$1 [L]
RewriteRule /home/(.*) /$1 [L,R=302]
RewriteOptions inherit
RewriteCond %{HTTP_HOST} ^test\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.test\.com$
but when i type test.com it shows a list of folders, not the content inside home.
What am i doing wrong?
thanks
You can simplify your rules to this in root .htaccess
RewriteEngine on
RewriteBase /
RewriteRule ^$ home/ [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((?!home/).+)$ home/$1 [L,NC]
Make sure to clear your browser cache before testing this.

how to hide file extension from the middle of the url using htaccess

I have a problem with hide URL in .htaccess. I want to remove .php from the URL.
For Example: convert www.example.com/destination_info.php/Laos-destination-trip to www.example.com/destination_info/Laos-destination-trip
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^./]+)/(.+)$ $1.php/$2 [L]
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^./]+)/(.+)$ $1.php/$2 [L]
To hide .php use following
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
To hide html
RewriteRule ^([^\.]+)$ $1.html [NC,L]
Try this, To remove the .php extension from a PHP file for example yoursite.com/wallpaper.php to yoursite.com/wallpaper you have to add the following code inside the .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
A reference to the full article can be found here
If you want to use the rule in the middle of the URL for an API like in my case, then use the first answer from #anubhava. That worked for me.
Also, I'm using IIS so I had to install the Rewrite Module and then Import the rule from the first answer. Second and Third answers will work only for the last part of the URL.

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]

Remove 'index.php' from URL with .htaccess

I've been trying all sorts of solutions from this site and none seem to work. I'm currently hosting with hostgator. This is my current .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
<IfModule mod_suphp.c>
suPHP_ConfigPath /home/user/php.ini
<Files php.ini>
order allow,deny
deny from all
</Files>
</IfModule>
This is in the root folder of my site. I have also tried adding a ? after index.php and no luck. Does anyone know why this isn't working?
This is the code you can use in your .htaccess (under DOCUMENT_ROOT) to remove index.php from URI:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]
Symfony 2 has an excellent solution:
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
# Sets the HTTP_AUTHORIZATION header removed by apache
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
RewriteRule .? %{ENV:BASE}/index.php [L]
This accomplishes the following:
RewriteBase is not necessary (useful when the site is in a subdirectory beneath the web root)
index.php is removed if present
The request is routed to the correct index.php with the full query string from the original request
Note that the line:
RewriteRule ^(.*) - [E=BASE:%1]
is responsible for setting the %{ENV:BASE} variable for later on. Refer to Apache documentation on E|env flag.
I tried this and works fine:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
Exceptions
If your site’s system directory (/system/) has been renamed and is still accessible by URL, modify the RewriteCond line above:
RewriteCond %{REQUEST_URI} !/newdirectoryname/.* [NC]
If you are running EE from a sub-directory rather from the root of your domain (e.g. http://example.com/myeesite/ instead of http://example.com/), just remove the slash preceding index.php in the RewriteRule line above, like so:
RewriteRule ^(.*)$ index.php/$1 [L]
If you are running EE from a sub-directory and it still doesn’t work after removing the slash, you may need to specify the sub-directory in your rewrite rule. For example, if your sub-folder is named testing, change:
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
To:
RewriteRule (.*?)index\.php/*(.*) testing/$1$2 [R=301,NE,L]
And change:
RewriteRule ^(.*)$ /index.php/$1 [L]
To:
RewriteRule ^(.*)$ testing/index.php/$1 [L]
If your host requires forcing query strings, try adding a question mark following index.php in the RewriteRule line above, like so:
RewriteRule ^(.*)$ /index.php?/$1 [L]
To remove index.php from urls on apache2.4 you can use the following rule :
RewriteEngine on
RewriteRule ^index\.php/(.+)$ /$1 [L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !index\.php /index.php%{REQUEST_URI} [L,END]
This will change the uri
/index.php/foobar
to
/foobar
This rule will return an internal server error for lower versions of apache as they don't support the END flag. Please see the anubhava's answer above that works almost on all versions.
I've been compelled to join stack overflow.com today to comment here as the answer has solved a long term problem I've had with a Zend Framework website. I've worked on the website around 3 1/2 years and during that time I discovered that it didn't handle index.php correctly and this causes webmaster tools to see duplicate titles etc.
I decided to search again today for a solution, one of many times attempted.
This is the one (the last answer shown above) that solves my problem.
# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
As an example. this is what it achieves for me.
http://www.domainname.co.uk/index.php/categories/url-name.html
becomes
http://www.domainname.co.uk/categories/url-name.html
Thank you to everyone who contributed to the original question as it lead to the answer and solution above.
Extra Note: I have other rewrite commands that handles the other aspects but those on their own didn't fix the index.php and the only time this has been fixed is by using.
# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
I only hope it helps others whether its ExpressionEngine or Zend Framework, in the future.