.htaccess only rewrite not redirect - .htaccess

I need a htaccess that will make the following URL:
domain.com/index.php?page=contact
Change the link into:
domain.com/contact
so if I'm doing:
Contact
The link will take me to
domain.com/index.php?page=contact
But the link shown in the link area on the browser will be:
domain.com/contact
can some one please help me?

This should work for you, it should be placed on your root folder where your index.php file is located inside a file called .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ index.php?page=$1 [L]
What the above rule does?
First we check if the file does not exist with:
RewriteCond %{REQUEST_FILENAME} !-f
Then we check if the folder does not exist with:
RewriteCond %{REQUEST_FILENAME} !-d
Then we check the URL is what we are looking for, in your case domain.com/anything which we do with:
^([^/]+)$
The above will collect anything that is after the first / after domain.com, for example:
domain.com/contact
domain.com/about-us
domain.com/home
The right most part of our rule before the square brackets, tells what to redirect and where to:
index.php?page=$1
Which means we want to redirect whatever we collected from the previous rule, above represented as $1 to index.php?page=$1 which using our previous example would mean:
domain.com/index.php?page=contact
domain.com/index.php?page=about-us
domain.com/index.php?page=home
[L] tells we want to stop, the L is a flag that means LAST, not having any redirection flags with it and the URL not being external or a full domain name, means we want to redirect internally.
RewriteBase / means we are on the root folder of your domain.
RewriteEngine On means we want to activate mod_rewrite which is responsible for the rewrite redirects.
-MultiViews is the only relevant Options in this case so I will only explain that, it takes care that when a folder does not exist, it doesn't look for its replacement to be served in its place.

Related

Rewrite url path to new path using .htaccess

in my htaccess file, i have this, that is supposed to remove the index.php? from the path and make the path look like this: video/:AccountId/:recordId however something isnt working correctly. it doesnt re-write anymore and i can only make the url work by having the index.php? tag in it, so instead it looks like: video/index.php?/:AccountId/:recordId
<IfModule mod_rewrite.c>
RewriteEngine On
# !IMPORTANT! Set your RewriteBase here and don't forget trailing and leading
# slashes.
# If your page resides at
# http://www.example.com/mypage/test1
# then use
# RewriteBase /mypage/test1/
RewriteBase /video
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
I cant just remove it because the clients already have the video/:AccountId/:recordId i dont want to have to email blast them to change something on their end to make this work, i was hoping too that maybe someone would know how to redirect that url to the one that includes the index.php? because overall i dont care that its there or not, its a hold over from previous teams.
With your shown samples, please try following htaccess rules file. Make sure your htaccess file is present along with video OR first level of URL folder, also make sure index.php is present inside that folder eg: `.htaccess and video are existing together AND index.php is inside video folder).
Make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/(.*)/?$ /$1/index.php?/$2/$3 [QSA,L]

Basic URL rewriting: How to have a css link, img src, etc, ignore it?

I have a website running at localhost/pm and the RewriteBase is correctly set to /pm/. There is a link tag in my document: <link ... href="themes/default/css/default.css">.
When the url is localhost/pm or localhost/pm/foo the CSS works all right. When there are more slashes in the URL, however, like localhost/pm/foo/bar the relative URL if the stylesheet changes to foo/themes/default/css/default.css.
How do I get this to work without having to put some sort of PHP path resolution in the link tag?
# invoke rewrite engine
RewriteEngine On
RewriteBase /pm/
# Protect application and system files from being viewed
RewriteRule ^(?:system)\b.* index.php/$0 [L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT]
EDIT:
Basically what I need now is this:
If request contains folder name /themes/ scrap everything that is before /themes/ and rewrite the rest to /pm/themes/...
I tried it like this: RewriteRule ^.*(/themes/.*)$ /pm/themes/$1 but I get an internal server error. Why?
If I do it like this: RewriteRule ^.*(/themes/.*)$ /pm/themes/ (ie. just remove $1 from the end) and use the URL http://localhost/pm/foo/themes/foo/ the resulting physical location is http://localhost/pm/themes which is what is expected too, which in turn means that at least my regex is correct. What am I missing?
The RewriteRule is almost correct
RewriteRule ^.*(/themes/.*)$ /pm/themes/$1
This rewrites http://localhost/pm/foo/themes/default/css/default.css to http://localhost/pm/themes/themes/default/css/default.css, which is one themes too much. Use this instead
RewriteRule /themes/(.*)$ /pm/themes/$1 [L]
But now you have an endless rewrite loop, because /pm/themes/.. is rewritten again and again. To prevent this, you need a RewriteCond excluding /pm/themes
RewriteCond %{REQUEST_URI} !^/pm/themes/
RewriteRule /themes/(.*)$ /pm/themes/$1 [L]
Now the request is rewritten only once and you're done.
You probably need to add the following lines before your RewriteRule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
It will only evaluate your rewrite rule if the requested file or directory doesn't exist.
You should post your .htaccess file so we can offer better advice

mod rewrite doing something weird

http://localhost/clean_urls/user/whtffgh redirect fine to this http://localhost/clean_urls/user/whtffgh/ (pretty much anything I put there works).
http://localhost/clean_urls/user/cohen however, redirects to this
http://localhost/clean/ and I have no idea why. It should just add a /
Heres my .htaccess code
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /clean_urls/user/$1/ [L,R=301]
RewriteBase /clean_urls/user/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9]+)/$ index.php?user=$1
</ifModule>
UPDATE:
The code works besides when I put cohen as the username. The second part of the .htaccess file is making user/index.php?user=username look like user/username so its just a $_GET variable that looks nice in the url and is SEO friendly (apparently).
I am just learning this stuff and making a webapp to try it out so not really sure where its going but I do think I will need another variable on the end. http:://localhost/clean_urls/user/fred/someverb
You misunderstand what the RewriteBase directive is used for. It helps the rewrite engine in a "per directory" context to convert the relative URIs to the correct filename equivalent.
You should only have one RewriteBase directive per .htaccess file. The rewrite engine by default will use the lowest .htaccess file on the path with the RewriteEngine On directive set.
So in DOCroot (that is the directory where a "GET /xxx" is mapped to, this should be
RewriteBase /
If your .htaccess file is in DOCroot/clean_urls/user then it should have
RewriteBase /clean_urls/user/
How do you want to decode URIs of the form http:://localhost/fred or are you only wanting do process URIs of the form http:://localhost/clean_urls/user/fred/. What about http:://localhost/clean_urls/user/fred/someverb?
Let me know and where you are putting you .htaccess then I can give you the right content.

mod_rewrite rule to match exact URL only

I'm having an issue with mod_rewrite where I want to match—and replace—a specific URL. The URL I want to rewrite is:
http://example.com/rss to http://example.com/rss.php
That means, if some one were to append anything after rss a 404 Not Found response be sent. Currently I'm using this mod_rewrite snippet:
Options -Indexes
RewriteEngine on
RewriteBase /
# pick up request for RSS feed
RewriteRule ^rss/?$ rss.php [L,NC]
# pass any other request through CMS
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+) index.php/$1
But this matches rss and rss with anything else added to the end. How can I re-write the above to acces only http://example.com/rss as the pattern for mod_rewrite to match against?
You are getting this error because /rss is being redirected twice in your rules by both RewriteRules. Have your rules like this:
Options +FollowSymlinks -MultiViews -Indexes
RewriteEngine On
RewriteBase /
# pick up request for RSS feed
RewriteRule ^rss/?$ /rss.php [L,NC]
# pass any other request through CMS
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (?!^rss\.php$)^(.*)$ /index.php/$1 [L,NC]
So with above rules it will redirect /rss OR rss/ URIs to /rss.php however /rss/foo will be redirected to /index.php since your 2nd rule is forwarding everything to /index.php
I was suprised to see that your rules just don't work, because in my first attempt I would have come to a very similar solution. But looking at the rewrite log revealed the real issue.
As discribed here the server prefers real files over directories. So internally rss/something becomes rss.php/something when applying the rewrite rules and things get weird.
So, one solution is to check if the Option MultiViews is enabled for the web directory either in .htaccess or in the vhost configuration. If so, remove it - which is what worked for me in this example.
If you need MultiViews, then I guess the only chance is to rename rss.php to rss-content.php and change the rule accordingly.
One additional note: you might want to add the following line after the # ... CMS block to prevent endless recursive calls.
RewriteRule ^index\.php/.* - [PT,L]
I hope this solves your rewrite problem.

What is a magic with 5 lines of code in htaccess?

I want to know, with this code in .htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Can do many things on query strings from URLs like Wordpres have done by example. It's depend on our coding or.. ? What is the secret ingredients to make like that?
Let me know if you have a good source to share with me. All in one.
This would make some kind of catch all page.
Let's explain it line by line
RewriteEngine On
enable mod rewrite
RewriteBase /
Set the base url of the rules to '/'
RewriteCond %{REQUEST_FILENAME} !-f
if the requested file doesn't exist (i.e not a jpg)
RewriteCond %{REQUEST_FILENAME} !-d
if the requested directory doesn't exist
RewriteRule . /index.php [L]
Redirect everything to index.php
So to summarize it will redirect any URL to index.php if the filename or directory requested doesn't exist.
and the documentation if you want to do some more reading.
The rewrite engine makes it possible to match paths and do manipulations, redirects etc. based on advanced rules.
Your specific example sets up two conditions (path must not represent an existing regular file nor directory) and then redirects anything else to index.php. You can then implement whatever you wish in index.php in order to match and/or parse the original request (e.g., /news/some-title could be forwarded to the news module where the news item can be fetched via the slug).
Sometimes you will see the initial redirect rewrite(s) done directly in .htaccess. The above could look like:
RewriteRule ^news/(.+)$ newsview.php?title=$1 [QSA,L]

Resources