mod_rewrite and hyperlinks - apache

I'm trying to get my head around mod_rewrite and friendly URLS.
OK, on a very basic level I have the following rule:
RewriteRule ^register$ register.php [L]
This allows me to browse to www.mydomain.com/register
The hyperlink within my pages shows register.php. Do I have to manually change my links to register?
Esentiallly, I do not want the .php extension on any of my links.
Thanks!!

Yes, you must manually change the hyperlinks (or use your favourite search-and-replace tool). mod_rewrite can't do this for you; it can only rewrite incoming requests, not outgoing HTML.

Yes, you'll need to change the URL in your code if that's not what you want to show up in the address bar.

Just an addition:
Note that RewriteRule ^(.*)$ /$1.php rewrites all files for you which saves you typing a lot of rules ;) Offcourse you can add more validation to it by using something like RewriteRule ^(.*)$ /index.php?pageId=$1.

Related

htaccess page to page redirect and seo friendly urls

i have a problem with a htaccess files and i cannot figure it what is the problem.
The site has url rewriting for seo purposes in place so:
www.website.com/page/seo-friendly-url
is rewritten to
www.website.com/page.php?seo=seo-friendly-url
this is done with the following
RewriteEngine on
RewriteBase /
Rewriterule ^page/([a-zA-Z0-9_-]+)$ page.php?seo=$1 [NC,L]
Now the problem is that i have to redirect some pages that are already indexed by the search engines to their new destination as they are no more available, for example:
www.website.com/page/seo-friendly-url
has to be redirected to
www.website.com/page/another-seo-friendly-url
I have tried something like this but it is not working
Rewriterule ^page/seo-friendly-url$ page/another-seo-friendly-url [R,NC,L]
also this one is not working
Rewriterule ^page/seo-friendly-url$ page.php?seo=another-seo-friendly-url [R,NC,L]
This seems pretty stupid but i can't find the problem :-/
Thank you for your help
Ema
Edit, for anubhava:
Hi,
no i have already set the rewriting for that.
What i'm trying to achieve is redirect an already rewrited link.
Let me explain myself better:
At the moment i have this url that is indexed by Google (or any other search engine) in the form of a beautified url (seo friendly). The url has this form:
www.website.com/page/seo-friendly-url
I have already set a rule in the htaccess so the previous link is rewritten and goes to a php page with a query string that is used to display some content.
The page and the query are in this form:
www.website.com/page.php?seo=seo-friendly-url
So basically i'm using the last part of the first url as a query parameter for the second url.
This is achieved (and works) through the following code here below:
RewriteEngine on
RewriteBase /
Rewriterule ^page/([a-zA-Z0-9_-]+)$ page.php?seo=$1 [NC,L]
So far so good.
Now what i need to achieve is to redirect this url, that has been deleted:
www.website.com/page/seo-friendly-url
to go to a new page
www.website.com/page/another-seo-friendly-url
Of course the same rules applies to this new url (www.website.com/page/another-seo-friendly-url -->is already rewrited to--> www.website.com/page.php?seo=another-seo-friendly-url)
What do i need to do to do the reewriting right?
Thanks
You need this extra rule before your existing rule:
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+page\.php\?seo=([^\s&]+) [NC]
RewriteRule ^ /page/%1? [R=301,L]
Rewriterule ^page/([\w-]+)$ page.php?seo=$1 [NC,L,QSA]
Just add redirects like this:
RewriteRule page/seo-friendly-url /page/new-url [R=301,L]
Important: this rules have to be above your existing rewrites because of the L flag in your rewrites
The [L] flag causes mod_rewrite to stop processing the rule set. In most contexts, this means that if the rule matches, no further rules will be processed.
http://httpd.apache.org/docs/current/rewrite/flags.html#flag_l
Edit
You want to redirect the old URL to avoid duplicate content (rewrite=internal, redirect=HTTP 301)
Maybe you are open for solutions thinking in another direction.
I would try to handle this in the application, no through rewrites. Right now the GET parameter seo is handled in page.php. Isn't it an idea to extend this in that way one product can be identified through multiple seo aliases? If one product has to be taken off a similar one will then own this alias (simply a change of one row in the database).
As I don't know what software you are using this may be not possible.

How to simulate directories with a htacess file (mod_rewrite)?

what I try to do is to simulate directories with the help of a htaccess file.
I have a website with a file like this:
http://www.domain.com/filename.php?t=yeah-a-title-2014
Now, I would like to rewrite the URL above to the following:
http://www.domain.com/directory1/yeah-a-title-2014/
If a visitor enters one of the two URLs, he should see the second one in his address bar but the content of the filename.php?t=yeah-a-title-2014 should be displayed.
I have no idea how to realize this.
Any ideas?
This is better known as SEO-urls (search engine optimized), SEF-urls (search engine friendly), fancy urls and a couple more of those terms. The basic problem with these kind of constructions, is that they cause an infinite loop if not implemented correctly, and therefore usually the THE_REQUEST trick is used, because %{THE_REQUEST} is always equal to the request, even if the url is rewritten, which in turn prevents the external redirect from matching if the internal rewrite matches.
#External redirect
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /filename\.php\?t=(.*)\ HTTP
RewriteRule ^ /directory1/%2/ [R,L]
#Change [R,L] to [R=301,L] after ALL rules do what you want them to do, and before your site goes live
#Internal rewrite
RewriteRule ^directory1/([^/]+)/?$ /filename.php?t=$1 [L]

Rewrite pages with a certain url to another htaccess

What I need to do it rewrite a url from one thing to another eg:
http://www.domain.com/page_one/blah //to
http://www.domain.com/page_two/blah
I've tried a few script I've found around the internet but I'm terrible with .htaccess and can never understand or get it right.
If you want to change only this specific url, use this:
RewriteEngine On
RewriteRule ^page_one/blah?$ http://www.domain.com/page_two/bla [L]
If you want to change the url for every file into the "page_one"-folder, this will help you:
RewriteEngine On
RewriteRule ^page_one/([^/]+)$ http://www.domain.com/page_two/$1 [L]
The RewriteEngine On activates the RewriteEnigine, so that you are able to use RewriteRules.
The ([^/]+) in the second solution means "every file, but no folders (the slash is excluded)". This is stored in $1 and used to create the new url.
Edit
[L] stops the script from using the other rules (if you have some)

What do these rewrite rules mean?

I subscribed to voltrank (website for building backlinks) and they told me to modify the .htaccess file with the following text:
RewriteEngine On
RewriteRule ^$ /vr_display_50727b.php?filename=index.html [L,NC]
RewriteRule ^(.*)\.html$ /vr_display_50727b.php?filename=$1.html [L,NC]
RewriteRule ^(.*)\.htm$ /vr_display_50727b.php?filename=$1.htm [L,NC]
My Question is: Is this safe and what does it mean? My main concern is security.
By the way, after modifying the .htaccess file, my stats showed that two offensive IP addresses had accessed my website. I'm not sure if the modification has anything to do with this or it is just coincident.
Thanks
It's a coincidence. What this does is cause all requests to execute vs_display_50727b.php instead.
What you should be concerned about is having your search ranks go down instead of up by using this program. Google does not like services designed to manipulate its rankings.
This is completely safe as it seems that your system is a kind of CMS which takes filenames as parameter to display the corresponding content. All this is redirecting your requests to vr_display_50727b.php page which decides which content will be drawn on page and same time helps you to have fancy URL with secure HTM /HTML filenames.
These rewrite rules increase page ranking

The correct way to make a rewrite rule?

I have a .htaccess in my site www folder that has this rewrite rule:
RewriteRule ^(\w+)/?$ /$1.php
It works, if you type in
http://sampardee.com/urltest -
It finds urltest.php and brings it up.
However, if you type in
http://sampardee.com/urltest/
it still brings urltest.php up but the CSS stops working. I have the CSS file specified in a link tag. The same results appear also when
http://sampardee.com/urltest.php/
is accessed.
Is there any way I can fix this so that someone could type in
http://sampardee.com/urltest/
and have urltest.php come up, but yet still display the linked CSS file?
Please help :)
-Sam
The problem isn't with mod_rewrite, but with the css link (the browser tries to fetch http://[...]/urltest/css/default.css instead of /css/default.css).
Try adding a beginning slash, and changing the to:
/css/default.css
A better idea would probably be to redirect http://sampardee.com/urltest/ to http://sampardee.com/urltest.
RewriteRule ^(\w+)/$ /$1 [R]
RewriteRule ^(\w+)$ /$1.php