First of all, I know there are plenty of similar questions about this around, but
None of them seem to work for me
None of them actually address exactly what I want
What I want is, as the title suggests, to redirect URLs without the .php extension to the actual .php file - changing the URL if possible (which I presume is just handled by [R=301]). The latest thing I tried was this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ $1.php [R=301]
That doesn't work. I can still cant access /about.php with /about. (.htaccess rules themselves are working fine though)
I understand RegEx fine, but htaccess rules just mess with my head =[
So what should I do?
Now I know what you're thinking
One of you will say this: "Why do you want to do this? Just get rid of extensions completely and access your pages via /about or /about/ with a trailing slash."
I'd like to do that, it looks quite good. Problem is SEO - from which I assume my page ranks will get annihilated because all of a sudden they're on different URLs. So before you suggest that, suggest how I'd keep my page ranks first.
What I'm actually doing is essentially URL shortening for a poster - it's a lot easier for people to remember mywebsite.com/about than mywebsite.com/about.php.
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 /
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=302,L]
# To internally forward /dir/foo/ to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
Please Make sure you have MultiViews options disabled using: Options -MultiViews
Beware of Apaches multiviews
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.
Please make sure that there's mod_rewrite on your Apache HTTP Server and try this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)$ /$1.php [R]
But clear your cache or use another browser first before checking the redirecting dynamic URLs, because you've been previously used the [R=301] flag! For more info. about that, please visit: https://stackoverflow.com/a/15999177/2007055
Could you try this one but it's quite the same as the previous code:
RewriteEngine on
RewriteRule ^([a-zA-Z0-9_-]+)$ http://%{HTTP_HOST}/$1.php
And when it works, try adding these two conditions above the rewrite rule:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)$ http://%{HTTP_HOST}/$1.php
And when any of these codes above does not work, I think there's a problem in your Apache HTTP Server.
That works for me.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]
You can chain it if you want e.g.
RewriteEngine On
# Remove trailing slashes.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=permanent,QSA]
# Redirect to HTML
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]
# Redirect to PHP
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]
# Redirect to ASP
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.asp -f
RewriteRule ^(.+)$ $1.asp [L,QSA]
Related
I want to add another language to my website (an app written in PHP 7).
I found out, good SEO practices say that every page on my site should be accessible from differend URLs, depending on the language.
Currently my .htaccess looks something like this:
RewriteEngine on
Options +FollowSymLinks
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([a-zA-z0-9-]+)$ $1.php [NC,L]
So when user types in (or clicks a link) http://example.com/contact they get page contact.php (if exists).
What I want to achieve is, to redirect http://example.com/en/contact to the very same file contact.php, but with $_GET argument and still redirecting /contact to contact.php (without this argument). I thought that would be:
... everything from above code sample and then:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^en/([a-zA-z0-9-]+)$ $1.php?lang=en [NC,L]
But it doesn't work. Any ideas why and how to make this work?
Last condition checks that en/file.php exists, which is never the case. That's why the rule is never met. Either you remove it (but it will be applied even on nonexistent files) or you use this workaround by rewriting the faulty condition
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^en/([^/]+)$ /$1.php?lang=en [NC,L]
To be more complete, you can also redirect users that try to access /contact.php?lang=en directly (better for SEO). Here is how your final htaccess should look like
RewriteEngine On
# if url is /file.php?lang=en and file exists then redirect to /en/file
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUEST} \s/([^/\s\?&]+)\.php\?lang=en\s [NC]
RewriteRule ^ /en/%1? [R=301,L]
# if url is /en/file and /file.php exists then internally rewrite to /file.php?lang=en
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^en/([^/]+)$ /$1.php?lang=en [NC,L]
Note: the above code is specific to en language, but you can easily adapt it to multiple languages
i want to change my website urls exemple www.mysite.com/about.php to www.mysite.com/about/
i'm using this code in my htaccess file
# Apache Rewrite Rules
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# Add trailing slash to url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)$ $1/ [R=301,L]
# Remove .php-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)/$ $1.php
# End of Apache Rewrite Rules
</IfModule>
i tried this one too
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
it works but all the page content becomes inside the /about/ its like i created a folder that does not exist so all the internal links are changed , if i have a link to post.php it goes to /about/post.php
all the included pages are not working too such as css and images
sorry my english isn't so good .
i finally found what i wanted ,i used this meta tag in the header section
`<base href="http://localhost/mysite/" />`
now everything works
ps: don't use localehost on webhost
I'm currently hosting a site on a service that has mod_rewrite on by default. I edited the .htaccess with
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
but it only allow the site to be accessed with "site.com/pagename." I want it to rewrite the url to say "site.com/pagename" rather than "site.com/pagename.html."
I've tried a million different flavors of this code and it won't rewrite the url to exclude .html. The HTML code has the pages linking to each other as
Text
If that makes a difference.
Thanks in advance for any help.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+\.)+html?\ HTTP
RewriteRule (.+)\.html?$ $1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
I am working on a twitter/facebook type site for a college class. Somehow they let a professor teach this class with no PHP, CSS, HTML, JavaScript,jQuery, or Ajax knowledge. I have been trying to rewrite my URLs to make them look like twitter. I have gotten all of my user profile pages to rewrite to: www.site.com/username from: www.site.com/profile.php?name=username. However, I also want to rewrite my login page, create account page, etc. Currently they are: www.site.com/login.html , www.site.com/createAccount.html. I want the to rewrite without the html. Here is my .htaccess file currently.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?name=$1 [L]
**Update
I apologize I ended up switching all of my files over to .php. But I am still having some issues. Now, I only want to redirect specific URLs. For example: www.361orc.info/login should internally redirect to www.361.orc.info/login.php . I cannot seem to figure out what is wrong with the following code. It redirects but it does it changes the client URL. I want it to just redirect internally. Here is my .htaccess file:
RewriteEngine On
#I want this code to change .com/login.php to .com/login but only internally
#the URL in the client's browser shouldn't change
RewriteCond %{REQUEST_FILENAME} !-
RewriteRule ^login?$ login.php [L]
#Change the profile pages of users from .com/profile.php?name=user to .com/user
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/?$ /profile.php?name=$1 [L,QSA]
you could do something like this
# Rewrite User Profiles
RewriteCond %{QUERY_STRING} ^name=([^&]+) [NC]
RewriteRule ^profile.php$ /%1 [R=301,L]
# Rewrite Login
RewriteRule ^login.html$ /login [R=301,L]
# Rewrite Create Account
RewriteRule ^createAccount.html$ /createAccount [R=301,L]
You are pretty close, just an external redirection rule that will redirect .html files to ones without .html extension:
RewriteEngine On
RewriteBase /
# To externally redirect /dir/file.html to /dir/file
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.+?)\.html[\s?] [NC]
RewriteRule ^ %1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^\.]+?)/?$ $1.html [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)$ profile.php?name=$1 [L,QSA]
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ([0-9a-zA-Z_\-]+)(|\.html|\.php)$ page.php
this is the rule is .htacess.
what I want is if file is not found then redirect to page.php.
for example if url people.php ,people.html is not found then redirect to page.php.
because I also want those filenames without .php like people direct to people.php first. then check file exist or not, if not redirect to page.php.
how do I add one rule to make people redirect to people.php
These rules should work for you:
RewriteEngine on
Options +FollowSymlinks -MultiViews
# if there is no . in request then append .php
# it is important to make [L] as last rule here
RewriteCond %{REQUEST_FILENAME} !\.
RewriteRule ^(.+)$ /$1.php [L]
# 404 handling for files that don't exist
# NC is for ignore case comparison
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[^.]+\.(html|php)$ /page.php [L,NC]
See if this does what you're looking for:
RewriteEngine on
RewriteRule ^(?:.+/)?([^/.]+)$ $1.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule \.(?:html|php)$ page.php