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
Related
I need to simultaneously do two things with htaccess.
I need to take a URL like:
http://client.example.com/123
and rewrite the directory to a param, and simultaneously add another subdomain to the url so it looks like this:
http://client.qa.example.com/?param=123
This does the param bit correctly, but I can't figure out how to add the subdir:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/[^/]+/?$
RewriteRule ^([^/]*)/?$ /?param=$1 [L]
You can examine the host header using a RewriteCond and extract the relevant parts of the name. Use them in the rewrite. Back references to matches in RewriteConds appear as %n
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} (.+?)\.(.*)
RewriteRule ^([^/]*)/?$ http://%1.qa.%2/?param=$1 [R,L]
(.+?)\.(.*) will do a match on everything up to the first . and then everything to the end. So client and example.com will respectively be in %1 and %2
If your .htaccess is in the root of client.example.com, it should be a simple redirect. Of course the directory has to be a fake directory or this won't redirect.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ http://client.qa.example.com/?param=$1 [R=301,QSA,L]
You can use the following to match (check for htaccess syntax):
(http://[^.]+\.)([^/]+/)([^/]*)/?$
And replace with:
$1qa.$2?param=$3
See DEMO
Finally got it working using:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} (.+?)\.(.*)
RewriteRule ^([^/]*)/?$ http://%1.qa.%2/?param=$1 [R,L]
Now I just have to figure out how to work in 2 parameters, given that param 2 isn't always going to be present.
This is my folder structure
htdocs
-> testing
->index.php
->.htaccess
Inside index.php I have this code
<?php
if(isset($_SERVER['PATH_INFO'])) {
echo $_SERVER['PATH_INFO'];
}
?>
Inside .htaccess I have this code
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php/$1 [NC,L]
Here is my question:
In the above htaccess rewrite rule, which will link my address localhost/testing/HelloWorld to localhost/testing/index.php/HelloWorld
Is it possible to do some thing like this? When the user enter localhost/testing/index.php/HelloWorld I want the browser to hide the word index.php and only display something like this localhost/testing/HelloWorld
I tried to redirect the link localhost/testing/index.php/HelloWorld to localhost/testing/HelloWorld by adding another rule in .htacces file but I get "The webpage has redirect loop" error message.
After I add in new rule the .htacces file looks like this
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php/$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^index.php/(.+)$ /testing/$1 [R]
Does any ways that I could make my link like this? Thanks in advanced, your help will be very much appreciated.
You get the redirect loop, because the rewritten url matches the rule for redirection. You'll need to use a trick to make the external redirect only happen on an external request with index.php, not on an internal rewrite that maps a different request to that file. You can do this with %{THE_REQUEST} which will only ever be whatever the external request was. It will not update if you rewrite the file it maps to.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php/$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /testing/index\.php/
RewriteRule ^index.php/(.+)$ /testing/$1 [R]
I have this rewrite rule that adds a .php extension to urls.
Url's entered look like this : http://localhost/Home
Url's read look like this : http://localhost/home.php
The problem i'm having is that some urls may look likes this:
http://localhost/sub/123
I should explain, this isn't actually a subdirectory. The sub part is the file and the 123 is used as a get variable. It looks cleaner this way.
If this happens you get a 500 error;
Here's what it is at the moment
#add php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
And here's what i've tried but doesn't work.
#add php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)|(.*)/(.*)$ $1.php
Change your code with this to solve your problem:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteCond %{REQUEST_URI} !^/(phpscripts|js)/ [NC]
RewriteRule ^([^/]*)(?:/(.*?|))?/?$ /$1.php?$2 [L]
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]
We are using an .htaccess in combination with anchor tags to serve files and conceal the server directory structure.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /_docu/clients/$1/$2/$3.pdf [NC,L]
For example, all files are stored under /public_html/_docu/clients/ and inside that folder are listed all the clients, and then under each client their projects. However, an anchor tag for a file would read only:
http://mydomain.com/client-name/proj-name/docname.pdf
(the /_docu/clients/ being omitted - there is a good reason for this). The above .htaccess grabs the client-name, proj-name and docname and serves it from the correct folder:
http://mydomain.com/_docu/clients/client-name/proj-name/docname.pdf
whilst preserving in the address bar the incorrect (concealed) directory structure.
I wish to handle the error condition of a document not existing. This should never happen, but it could. Can anyone suggest a way of dealing with this? Can something functionally akin to "if fileexist($1/$2/%3.pdf)" be somehow constructed in an .htaccess file?
EDIT:
Delayed response as JL's answer below required research and experimentation. Thanks, Jon, for the gentle push in the right direction but I haven't got it to work just yet. Here's what I tried:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# check if the requested file exists in the "_docu/clients" directory
RewriteCond %{DOCUMENT_ROOT}/_docu/clients/$1/$2/$3.pdf -f
RewriteRule ^([a-z0-9])/([a-z0-9])/([a-z0-9]*).pdf$ /_docu/clients/$1/$2/$3.pdf [NC,L]
RewriteRule ^(.*)$ /errors/404.php [L]
I thought that what that should do is:
If http://mydomain.com/_docu/clients/$1/$2/$3.pdf does not exist,
GoTo page http://mydomain.com/errors/404.php
Actual outcome is an "internal server error" message.
EDIT TWO:
Latest changes:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/([a-z0-9])/([a-z0-9])/([a-z0-9]*).pdf$
RewriteCond %{DOCUMENT_ROOT}/_data/cli/%1/%2/%3.pdf -f
RewriteRule ^([a-z0-9])/([a-z0-9])/([a-z0-9]*).pdf$ /_data/cli/$1/$2/$3.pdf [NC,L]
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule ^(.*)$ /metshare/404.php [L]
The problem with this one is that legitimate pages also are directed to 404.php
MESSAGE TO FUTURE READERS:
All of the above concerns were addressed in Jon Lin's final answer. As issues were detected, he modified his answer until it was a perfect, working solution. I am leaving the above as it is because there are some good ULOs within (unscheduled learning opportunities) for those who want to compare versions.
You need to use a condition like this:
RewriteCond %{DOCUMENT_ROOT}/_docu/clients%{REQUEST_URI} -f
So that your rules looks ssomething like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# check if the requested file exists in the "_docu/clients" directory
RewriteCond %{DOCUMENT_ROOT}/_docu/clients%{REQUEST_URI} -f
RewriteRule ^ /_docu/clients%{REQUEST_URI} [L]
EDIT: Response to edit in question
You can't do this:
RewriteCond %{DOCUMENT_ROOT}/_docu/clients/$1/$2/$3.pdf -f
Because the backreferences for $1/$2/$3 don't exist yet, they are matched in the groupings in your RewriteRule, which hasn't happened yet at this point. But you can try something like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# check if the requested file exists in the "_docu/clients" directory
RewriteCond %{REQUEST_URI} ^/([a-z0-9])/([a-z0-9])/([a-z0-9]*).pdf$
RewriteCond %{DOCUMENT_ROOT}/_docu/clients/%1/%2/%3.pdf -f
RewriteRule ^([a-z0-9])/([a-z0-9])/([a-z0-9]*).pdf$ /_docu/clients/$1/$2/$3.pdf [NC,L]
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /errors/404.php [L]
Essentially creating a match against %{REQUEST_URI} in a previous RewriteCond then using the %N backreferences in the following RewriteCond.