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!
Related
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.
I want to remove the default index.php that auto comes with Codeigniter.
I've been able to remove that with this code
RewriteRule ^(.*)$ index.php/$1 [L]
example.com/index.php/blog can now be accessed by example.com/blog
I later wanted to prefix the URL with a www i.e example.com/blog should redirect to www.example.com/blog with these rewrite rules
RewriteCond %{HTTP_HOST} ^(?!www\.)(.+) [NC]
RewriteRule ^(.*) http://www.%1/$1 [R=301,NE,L]
After adding this code above to the end of my .htaccess file, it began to misbehave.
If I enter www.example.com/blog into a URL bar it works fine but if I enter example.com/blog it redirects to www.example.com/index.php/blog
What I'm I doing wrong?
I want example.com/blog to redirect to www.example.com/blog
Note: I am using the Codeigniter framework.
Added: This code is just on top of the previous ones I have up. Maybe this is it's problem please HELP!!!
RewriteCond $1 !^{index\.php|[assests/images/themes/fonts/style/scripts/js/install]|robot\.txt|favicon\.ico}
Try this:
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ example.com/$1 [L,R=301]
After much tricks and following the post given by #Tpojka I was able to come up with this
RewriteEngine On
RewriteBase /
# Removes trailing slashes (prevents SEO duplicate content issues)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]
# no www -> www
RewriteCond %{HTTP_HOST} ^(?!www\.)(.+) [NC]
RewriteRule ^(.*) http://www.%1/$1 [R=301,NE,L]
# Checks to see if the user is attempting to access a valid file,
# such as an image or css document, if this isn't true it sends the
# request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
This guy was my problem
RewriteCond $1 !^{index\.php|[assests/images/themes/fonts/style/scripts/js/install]|robot\.txt|favicon\.ico}
I would need help on how to exclude folders and some files like robot.txt file like I tried in the malfunctioning line.
This is the complete .htaccess file which I use personally:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
I am creating a social networking site. Using .htaccess I have successfully removed .php extensions from urls but I am not able to redirect domain.com/u.php?username=[username] to domain.com/u/[username]. Following is my .htaccess file:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
DirectoryIndex home.php
I have seen many examples and codes in stackoverflow but all of them redirect from domain.com/user.php?username=[username] to domain.com/[username]. But this would affect my website since I have removed .php extensions and I might end up with name clash. Thanks in advance.
You can put this code in your root htaccess
DirectoryIndex home.php
Options -MultiViews
RewriteEngine On
RewriteBase /
# redirect /u.php?username=XXX to /u/XXX
RewriteCond %{THE_REQUEST} \s/u\.php\?username=([^&\s]+)\s [NC]
RewriteRule ^ u/%1? [R=301,L]
# hide php extension (if file exists)
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUEST} \s/(.+)\.php(?:\?|\s) [NC]
RewriteRule ^ %1? [R=301,L]
# internally rewrite /u/XXX to /u.php?username=XXX
RewriteRule ^u/([^/]+)$ u.php?username=$1 [L]
# internally rewrite (if it exists) extensionless to php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(.+)$ $1.php [L]
I have files in my root folder. jobs.php, contact.php.
I want to be able to use .htaccess to remove the .php extension and force a trailing slash /. So that, www.domain.com/jobs/ and www.domain.com/contact/ goes to their respective pages.
Also, I want jobs.domain.com to point to www.domain.com/jobs/
<IfModule mod_rewrite.c>
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteBase /
RewriteRule ^(rosquillos)/$ $1 [L,R=301,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
</IfModule>
This is what I've got so far, and as much as I try. I can't seem to get it to work.
Any help would be awesome.
EDIT:
I would like to rephrase my question. The existing code removes the .php extensions for me, what I really only need right now is an additional rule that points the subdomain to the correct file: ie. jobs.domain.com points to jobs.php. I can live without the trailing slash.
Your condition:
RewriteCond %{REQUEST_FILENAME}\.php -f
will fail if your URL's end with a trailing slash. But if you only have jobs and contact then you'll just want:
RewriteEngine On
# add trailing slash and redirect browser
RewriteRule ^([^/]+)$ /$1/ [L,R=301]
RewriteRule ^([^/]+)/$ /$1.php [L]
# redirect direct access to php files:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+)\.php
RewriteRule ^ /%1/ [L,R=301]
Oh and I forgot:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$ [NC]
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^ /%1.php [L]
Assuming that *.domain.com has the same document root as www.domain.com.
Pages are currently available at the following addresses:
http://www.domain.co.uk/solutions/article/page-name1
http://www.domain.co.uk/solutions/article/page-name2
http://www.domain.co.uk/solutions/article/page-name3
etc
..but I would like them to be accessible via the following addresses in order for enhanced SEO:
http://www.domain.co.uk/solutions/page-name1
http://www.domain.co.uk/solutions/page-name2
http://www.domain.co.uk/solutions/page-name3
So, I think I want to use mod_rewrite in the .htaccess file of the site root to add the 'article' directory after the solutions directory on each incoming page request. I have been looking for a suitable answer for about a month and I've tried learning the mod_rewrite basics tutorials but I just can't make this work for me so apologies for another mod_rewrite question.
This is my .htaccess file at present:
<IfModule mod_rewrite.c>
RewriteEngine On
# Removes index.php
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
# If 404s, "No Input File" or every URL returns the same thing
# make it /index.php?/$1 above (add the question mark)
</IfModule>
I'm doing something similar on a site of mine. Here's the .htaccess rules I'm using to remove index.php and show the shortened url:
RewriteEngine On
# Removes index.php
RewriteCond $1 !\.(css|js|gif|jpe?g|png|ico) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1
RewriteCond %{REQUEST_URI} ^/solutions/(.*) [NC]
RewriteRule ^(.*) /solutions/article/%1 [L]
Remember that the URL you see in your browser's address bar is not the URL that ExpressionEngine sees - EE sees {segment_2} as "article".
Edit :
Try this :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^solutions/article/
RewriteRule ^solutions/(.*)$ /solutions/article/$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !\.(gif|jpe?g|png)$ [NC]
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>