.htaccess redirect /dir to /index?page=dir - php

I'm looking to redirect example.com/subdir to example.com?page=subdir, while still keeping example.com/subdir in the URL. This way, I can dynamically "create pages" with different get requests. I've gotten down how to do this, but I can't seem to figure out how to prevent example.com/subdir/subdir from causing assets to load improperly. Here's my current code:
Options +FollowSymLinks
RewriteEngine on
# Redirect /page.php, /page.html, and /page.htm to /page
RewriteRule ^(.+)\.(php|html|htm)\/*$ /$1 [R,L]
# If the page is not a file or directory
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-d
# Internally redirect /page to /index.php?page=page
RewriteRule ^(.*?)/?$ /index.php?page=$1 [NC,END]
# Redirect /index to /
Redirect /index /

Keep only these rules in your .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index|(\S+?))\.(?:php|html)[/\s?] [NC]
RewriteRule ^ /%1%2 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*?)/?$ index.php?page=$1 [L,QSA]
Make sure to use a new browser for your testing to avoid old cache.

Related

Removed .php extension but now it says page does not exist

I was able to get the .php off my files but now when I go to the pages they do not exist(and it's forcing a WordPress 404 page when the target files are actually on the server outside of WordPress). I think the problem might be that it's on a server with WordPress and these files lie outside of word press. There's already rewrite rules for WordPress and maybe it's conflicting with my rewrite rules for other files?
.htaccess file:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
# Remove .php extension on files outside of wordPress
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.+)/$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^(.*)/$ $1.php [L]
# Force trailing slash
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ $0/ [L,R=301]
UPDATE - Full .htaccess
# BEGIN WP Hide & Security Enhancer
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#WriteCheckString:1548706048_68906
RewriteRule .* - [E=HTTP_MOD_REWRITE:On]
RewriteRule ^site(.*) /wp-login.php$1 [L,QSA]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^wp-login.php /index.php?wph-throw-404 [L]
RewriteCond %{REQUEST_URI} /enter$
RewriteRule ^(.*)$ /enter/ [R=301,L]
RewriteRule ^enter(.*) /wp-admin$1 [L,QSA]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^wp-admin(.+) /index.php?wph-throw-404 [L]
</IfModule>
# END WP Hide & Security Enhancer
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
You need to do something like this
#remove all PHP extensions from the url
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301] #end and redirect
# Force trailing slash ( probably no longer needed, because `/$1/` )
#RewriteCond %{REQUEST_FILENAME}.php -f
#RewriteRule .*[^/]$ $0/ [L,R=301]
#add .php back in and continue.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.+)/$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^(.*)/$ $1.php #no last flag as we can now continue to WP with our nice php extensions back on
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I'm assuming those rewrite rules are correct. I would have to look them up I haven't worked heavily with Rewrites in a few years. Basically
first remove any php extensions and redirect (if they exist)
on a redirect (or a request without .php) add back the php extension to those urls where it was removed so you may need %{REQUEST_FILENAME} !-d just to be safe.
carry on as normal
Just as a general rule of thumb, it's best to do redirects first. That way you can get to the redirect with the shortest amount of work. A redirect is like changing the url and then putting that in the browser and running it again (ie. a brand new request).
Another way to do this is to redirect all requests to a certain folder to an index.php of your making that has a router.
Such as this one (that I made as an example)
https://github.com/ArtisticPhoenix/MISC/tree/master/Router
Then your URL's can be like this
http://yourdomain.com/somedir/index.php/controller/method/...args
#or without the index.php
http://yourdomain.com/somedir/controller/method/...args
Then in HTACCESS all you would do is remove index.php
#remove index.php and redirect
RewriteRule somedir/index.php/(.+)/?$ /somedir/$1/ [L,R=301] #end and
#add index.php back in
RewriteRule somedir/(.+)/?$ /somedir/index.php/$1/ [L]
//wordpress stuff
The nice thing about routers is you never have to touch Htaccess, and with a folder name to key in on it should make things simpler.
Using the router and the example files:
http://yourdomain.com/somedir/user/login
Should go to Controllers/user.php method login.

Remove index,index.php error handle with trailing slash PHP Htaccess

I need some help with my Htaccess code.
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule (.*)\.php$ $1 [R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L,QSA]
My Directory Structure is:
Public_Html
css/style.css
img/icon.png
/2/user.php
index.php
create.php
Now the above htaccess code is removing the .php extension from the URL.
Examples
-> example.com/index.php -> example.com/index
-> example.com/2/user.php -> example.com/2/user
Problems with this Htaccess File
I want to hide the index too
Example: example.com/index -> example.com
If user types example.com/index.php then he should be redirected to example.com
If user type things which do not exist then it should point to example.com
Example: example.com///*/// -> example.com
When it removes the .php extension then a trailing slash should be added to the end.
Example: example.com/create.php -> example.com/create/
If a user wants to access create.php and enter /create without / at the end then it should be automatically added.
Example: example.com/create -> example.com/create/
In other words, if after removing .php these both works as same
**Example:** example.com/create = example.com/create/
But user should still be able to access the directory
Example: example.com/2 -> example.com/2/
Here 2 is a folder.
I have some problem with this htaccess script. I searched a lot on Google and Stack Overflow but still can't find any solution.
*This is not a duplicate question. Please check it carefully before marking it as a duplicate. I would be glad if you help me.
Here is everything you need, all questions are answered with code, information, and examples! Providing 1 by 1 step for what each does and then the whole code!
1-) Resolving .php extension
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
example.com/user.php will be working if you go to example.com/user
2-) Redirecting wrong links that don't exist to example.com
If a user types a link which doesn't exist, for example, example.com///*, the user will be automatically redirected to example.com
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
3-) Redirect example.com/index.php or example.com/index to example.com
RewriteCond %{THE_REQUEST} /index(\.php)?[/?\s] [NC]
RewriteRule ^(.*?)index(?:\.php)?$ /$1 [L,R=301,NC,NE]
4-) Remove trailing slash if it is not a directory else add
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=301,L]
If you have a trailing slash added after a normal file, this will remove it. If it is a directory and has, it will remain. If it doesn't have, a trailing slash will be automatically added to it.
You will be able to access directories!
5-) Removing .php from URL ( Redirect to file name without .php )
RewriteCond %{THE_REQUEST} \.(php|cfm)[\s?] [NC]
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)\.(cfm|php)$ /$1 [R=301,L,NE]
The whole code together for .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=301,L]
RewriteCond %{THE_REQUEST} /index(\.php)?[/?\s] [NC]
RewriteRule ^(.*?)index(?:\.php)?$ /$1 [L,R=301,NC,NE]
RewriteCond %{THE_REQUEST} \.(php|cfm)[\s?] [NC]
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)\.(cfm|php)$ /$1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
I hope this has helped you.
Happy to help, enjoy!

htaccess redirect fails with multiple URL parameters

I've got the following htaccess file:
RewriteEngine On
RewriteBase /
# Redirect to remove .php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
# Redirect to "page" for dynamic pages
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ page?url=/$1 [L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC,L]
This allows my custom CMS to use dynamic URLs (http://example.com/some-page, for example) and redirect it to http://example.com/page?url=some-page so that the CMS can render the content. It all works great - until someone adds a URL like http://example.com/something/else. When I spit out the url parameter with: print $_GET['url']; I get /something/else.php/else.
So it seems like the remove .php directive is getting lost and the second parameter is getting duplicated? Thanks for any help.
Have it this way:
Options -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,NE,L]
RewriteCond %{ENV:REDIRECT_STATUS} =200
RewriteRule ^ - [L]
# Redirect to remove .php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^/]+)/?$ $1.php [L]
# Redirect to "page" for dynamic pages
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule !^page\.php$ page.php?url=%{REQUEST_URI} [L,NC,QSA]
Here are changes:
Keep redirect rule before rewrite rules otherwise when www is removed from a URL then your internal URL will be exposed.
Use page.php in target instead of page to avoid another rewrite rule execution.
Use [L] flag in .php adding rule.
Addition of Options -MultiViews

.htaccess redirect peaple friendly url with parameters

I try to make the URL of my site a litle more readable, but I have trouble doing this.
Her are samples of the URL it Looks like now:
http://example.com/
http://example.com/index.php
http://example.com/index.php?page=home
http://example.com/index.php?page=profile&uid=483ec3a0-7e0f-32e6-f357-ac9311204246
They have to look like:
http://example.com/
http://example.com/home/
http://example.com/profile/483ec3a0-7e0f-32e6-f357-ac9311204246/
Here is the Content of my .htaccess file:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# external redirect from /view.php?id=1 to /view/id/1
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?page=([^\s&]+)&uid=([^\s&]+) [NC]
RewriteRule ^ /%1/%2/? [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?page=([^\s&]+) [NC]
RewriteRule ^ /%1/? [L,R=301]
# internal forward from /view/id/1 to /view.php?id=1
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?page=$1&uid=$2 [L,QSA]
RewriteRule ^([^/]+)/?$ /index.php?page=$1 [L,QSA]
It rewrites correct an URL like http://example.com/index.php?page=profile&uid=483ec3a0-7e0f-32e6-f357-ac9311204246 -> http://example.com/profile/483ec3a0-7e0f-32e6-f357-ac9311204246/ but i get everytime an 404 error. If i try to call http://example.com/ or http://example.com/home/ i get an 500 error.
What is my problem?
You need another redirect rule for no parameters and you need to add some conditions to your internal forwarding rules so that /index.php doesn't get matched:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# external redirect from /view.php?id=1 to /view/id/1
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?page=([^\s&]+)&uid=([^\s&]+) [NC]
RewriteRule ^ /%1/%2/? [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?page=([^\s&]+) [NC]
RewriteRule ^ /%1/? [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php($|\ ) [NC]
RewriteRule ^ /? [L,R=301]
# internal forward from /view/id/1 to /view.php?id=1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?page=$1&uid=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /index.php?page=$1 [L,QSA]
It seems you're hitting problem of infinite looping causing 500. It is due to the reason that last 2 routing rules aren't skipping files and directories.
Insert this rule just above # internal forward line:
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

Remove/Redirect index.php from url to prevent duplicate urls

Please read the question carefully before marking as duplicate.
We all know, that using in .htaccess:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
we can redirect all traffic to index.php so we can create friendly urls and have one front controller.
Although the question is connected to mod_rewrite the problem is described for Laravel.
The following .htaccess comes by default with Laravel 4 and it works fine:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
If we run url mydomain.com/something and have set that route for something properly, some controller will be launched. It works fine so far.
However in Laravel 4 we will be able to reach the same route using mydomain.com/index.php/something. Probably using Laravel url creating we will have no urls with index.php in url but there is some other problem.
For example if our competition would like to make us some harm, they can simple put in Internet single links for urls to mydomain.com/index.php/something, mydomain.com/index.php/something2 and so on and search engines will see duplicate urls.
Of course if we have our custom PHP application, we can do it in PHP without a problem checking simply $_SERVER['REQUEST_URI'] and make 301 redirection. We can of course do the same in Laravel but we have to write this code in PHP each time and probably some developers could say it is bad practice to do it in PHP.
Question is simple: how can I redirect in .htaccess all urls that contain index.php to to the same url without index.php?
Example urls that should be redirected:
mydomain.com/index.php/something should be redirected to mydomain.com/something (something could be anything - can contain any characters)
mydomain.com/index.php should be redirected to mydomain.com
mydomain.com/index.php?anything should be redirected to mydomain.com (anything can contain any characters)
mydomain.com/index.phpanything should be redirected to mydomain.com anything can contain any characters)
Insert these rules just below RewriteEngine On line:
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php[^/] /$1? [L,R=302,NC,NE]
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php(?:/(.*))?$ /$1$2? [L,R=302,NC,NE]
After spending hours I write below code for me and its 100% working
Redirect index.php to non index.php
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^index.php/(.*)$ /$1 [R=301,L]
how can I redirect in .htaccess all urls that contain index.php to to
the same url without index.php?
Add this to your .htaccess
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]
For Nginx, here is the rules :
location / {
rewrite ^/(.*?)index\.php[^/] /$1? redirect;
rewrite ^/(.*?)index\.php(?:/(.*))?$ /$1$2? redirect;
}
This solved my problem to force https & remove index.php from the url in Kohan 2.3
RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php[^/] /$1? [L,R=302,NC,NE]
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php(?:/(.*))?$ /$1$2? [L,R=302,NC,NE]
RewriteRule ^(application|system) - [F,L]
RewriteCond %{THE_REQUEST} /index.php [NC]
RewriteRule ^(.*)index\.php$ /$1/ [R=301,L,NC,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?!localhost$|127\.0\.0\.1$)(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]

Categories