URL rewrite rule that allows long URL to appear shorter - apache

I would like to users to visit:
domain.com/example
and have the content from the following page displayed:
domain.com/directory1/directory2/directory3/example.html
In the browser, I would like it to say domain.com/example without redirecting. We have a series of landing pages that need to have short URLs within the domain.
I considered doing something programmatically such as a PHP include however I felt an .htaccess rewrite rule would be best practice.

So you want:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^example$ /directory1/directory2/directory3/example.html [L]
If you want it to work for every file (replacing "example" with any file) then replace the RewriteRule line with:
RewriteRule ^([^/]+)$ /directory1/directory2/directory3/$1.html [L]

Related

Redirecting all urls, including no path, to a file in subdirectory

I have checked a large amount of existing answers regarding .htaccess redirects. However none of them have helped me.
What I want to accomplish is redirecting all request urls to /api/init.php. However I've only gotten so far to where my index page www.example.com simply gives me a file listing because of the missing index.php file, while every url request with a path is working.
How can I accomplish this with .htaccess without ending up with a directory listing on my landing page?
This is as far as I got:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /api/init.php?path=$1 [NC,L,QSA]
Well your site root is a directory, so this rule you have excludes existing directories. What you could do is only exclude existing files, and allow existing directories to be handled by the PHP script. Like this:
RewriteEngine on
RewriteCond %{REQUEST_URI} !=/api/init.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /api/init.php?path=$1 [L,QSA]
I removed the NC flag as it's not needed. I added a condition to prevent an unnecessary file-system check.
You don't have to pass the path on in a URL parameter, as you could get it from $_SERVER['REQUEST_URI'] in PHP (not the same as REQUEST_URI in mod_rewrite, in PHP it always has the original URI). If you wanted to do that then your rule becomes nice and simple:
RewriteEngine on
RewriteCond %{REQUEST_URI} !=/api/init.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /api/init.php [L]
Because the query string will just be passed on unaffected (so QSA is not needed).

htaccess rewrite .html not required / is optional

I have a working website, with atleast 500 pages ranked in Google.
All pages have .html at end of page.
Now I want to remove .html of all pages, but let the pages in Google (with .html) keep there index.
After searching I cant find the correct answer.
I know the ? is for optional. I tried 2 Rules behind eachother but didnt work too.
Here is what my htaccess now is:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*).html$ find_page.php?redirect=$1&%{QUERY_STRING} [L,QSA]
I tried with adding:
RewriteRule ^(.*)$ find_page.php?redirect=$1&%{QUERY_STRING}
So if URL contains no extension use this rule, else use the normal rule (with htaccess)
I should expect my rule should be something like this: ^(.*)(?\.html)$
So my goal is: With or without html should work, but .php shouldnt be work :-)
Why look for a complex solution?
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)\.html$ find_page.php?redirect=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)?$ find_page.php?redirect=$1 [L,QSA]
This rewrites all request to that php script, adding the original "file name" as parameter "redirect" and preserves all query parameters. That is what you asked for in your question.
But a warning: you can do this and it will allow to rewrite requests to for example page "redirection" as .../redirection?somearg or .../redirection.html?somearg. But for google both request are completely different pages. This will not help you to preserve any ratings when shifting to the new request scheme.
And a general side note: if you have control over the http server configuration, then you should always prefer to place such rules in the hosts configuration instead of using .htaccess style files. Such files are notoriously error prone, make things complex, are hard to debug and really slow the server down. They should only be used in two cases: if you do not have control over the http server configuration or if you require your scripts to do dynamic changes to your ruleset (which is always a very insecure thing).
Ok solved my problem.
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.html([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.html find_page.php?redirect=$1&%{QUERY_STRING} [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ find_page.php?redirect=$1&%{QUERY_STRING} [L,QSA]
With this option there will be checked if the page has .html optional at end. If it has, will the first rule be matched, else will go further and use the second rule which has no html at the end
Try
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.html
You don't need find_page.php for redirection. As it mentioned in other answer http://server/folder/file and http://server/folder/file.html becomes the same for the user but different for the Google.
This does not affect to PHP, folders and other content. It just tries to add «.html» to requested URL if it does not point a file or folder.
I've checked, it works fine even user queries uri with anchor like 1.html#bookmark1

mod_rewrite rule for replacing string in URL

I would like to replace a string from old URL:
http://www.homepage.com/projectname/public/ActionName.action
to
http://www.homepage.com/ActionName.action
or even
http://www.homepage.com/short
I got to this point (trying to it get shorten to http://www.homepage.com/ActionName.action)
RewriteEngine On
RewriteRule ^/projectname/public/(.*)$ http://www.homepage.com/$1 [R=301,L]
But I get a 404 error:
The requested resource (/projectnameActionName.action) is not available.
I think you're looking for an internal redirection.
So if you want to be able to put URL's like this in the browser:
http://www.homepage.com/ActionName.action
But show a page like this:
http://www.homepage.com/projectname/public/ActionName.action
Then use this code in your.htaccess.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+) http://www.homepage.com/projetname/public/$1 [NC,L]
Of course replace the domain name and folders to yours.

RewriteCond Being Ignored?

I am trying to use mod_rewrite on a Ubuntu 12.04 server to make my URLs more readable, however I want to add an exception for images and css files.
My input URLs are in the format \controller\action which is then re-written to index.php?controller=controller&action=action. I want to add an exception so that if an image or css file is specified, the URL is not re-written, e.g. \images\image.jpg would not be re-written.
My .htaccess code is as follows:
RewriteEngine on
RewriteCond %{REQUEST_URI} !(\.gif|\.jpg|\.png|\.css)$ [NC]
RewriteRule ^([a-zA-z]+)/([a-zA-z]+)$ test.php?controller=$1&action=$2 [L]
RewriteRule ^([a-zA-z]+)/([a-zA-z]+)/([^/]*)$ test.php?controller=$1&action=$2&$3 [L]
My re-write code is working fine and the URLs are coming out as intended, however even if I request an image, the URL is still being re-written. It appears that my RewriteCond is being ignored, anyone any suggestions as to why this might be?
The RewriteCond only applies to your first RewriteRule, it should be reproduced for the second rule. However, I think that is better to add a non-rewriting rule, before, to exclude existing stuffs.
# Do nothing for files which physically exist
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
# your MVC rules
RewriteRule ^([a-zA-z]+)/([a-zA-z]+)$ test.php?controller=$1&action=$2 [L]
RewriteRule ^([a-zA-z]+)/([a-zA-z]+)/([^/]*)$ test.php?controller=$1&action=$2&$3 [L]
The rewriteCond rule is only applied for the next RewriteRule.
So you need to at least repeat the rewriteCond for your seconde RewriteRule.
No there is certainly better things to do.
For example a usual way of doing it is to test that the url is matching a real static ressource. If all your php code is outside the web directory (in libraries directory, except for index.php) then all styatic ressources available directly on the the document root can only be js files, css files, or image files.
So this is the usual way of doing it:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-z]+)/([a-zA-z]+)$ test.php?controller=$1&action=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-z]+)/([a-zA-z]+)/([^/]*)$ test.php?controller=$1&action=$2&$3 [L]
But this is a starting point. We could certainly find something to avoid doing 2 rules for this (maybe I'll have a look later)

Redirect sitename.com/en to sitename.com/ar using htaccess file

I want to redirect sitename.com/en and all links inside that sections to the same urls with /ar instead of /en using htaccess file. The number of links inside the section is enormous, so I can't write them one by one. Please help...
Use this .htaccess :
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^en/(.*)?$ /ar/$1 [QSA,L]
If you want that old pages (en/) to no longer be used, and see the new url (ar/) in your browser, use [QSA,L,R=301] instead of [QSA,L] (it notify you're using a permanent redirection)