How exactly does mod_rewrite work when displaying old url - php

I have used mod_rewrite to change http://mywebsite.com/?page=2 to http://mywebsite.com/page/2 using the following code:
RewriteEngine On
RewriteRule ^page/([^/]*)\.php$ /?page=$1 [L]
But when i type in the old url - http://mywebsite.com/?page=2 the page still appears and the url doesnt change to the static one. Is mod-rewrite meant to redirect the user or just make everything on the dynamic url appear on the static one when entered? If so how can i redirect any user that goes to the old dynamic url - http://mywebsite.com/?page=2 to the new static one http://mywebsite.com/page/2? Plus does google still index the old url?
This is my complete .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
# external redirect from actual URL to pretty one
RewriteCond \s/+\?page=([^\s&]+) [NC]
RewriteRule ^ /page/%1? [R=301,L]
# existing rule
RewriteRule ^page/([^/]+)/?$ /?page=$1 [L,QSA]

Have your full .htaccess like this:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+\?page=([^\s&]+) [NC]
RewriteRule ^ /page/%1? [R=301,L]
# existing rule
RewriteRule ^page/([^/]+)/?$ /?page=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [L]

mod_rewrite necessarily will not redirect the visitor to the new fancy URL, but it will make sure that anybody who visits the new URL will still access it as if it is using the old URL
I believe you can achieve something like that with mod_substitute, but that's not something I will advice you to do now.
If you can, manually change the links to reflect the new changes, or have a function which will parse out your links such as
http://mywebsite.com/?page=2 to http://mywebsite.com/page/2
such as
function fancyuri($url){
//blah blah, strip out ? and change all occurence of equality sign = to forward slash
//do some other tricks
//return formatted links
}
And then pass your links as a param to the function and have it return your nice urls, in this case, even if you don't want it again, you can strip off the formatting codes in the function and still have ur original url returned.

Related

.htaccess keep multiple GET params

Part 1 takes care of making example.com/fr behave like example.com?lang=fr, or example.com/fr/some-page.php like example.com/some.page.php?lang=fr etc.
Part 2, which I'm currently working on not working well yet, is to obtain a new GET param for other pages called page, in this case if there's login in the url.
Problem: It seems like part of the page loads twice when going to for example example.com/login or example.com/fr/login.
Maybe un-necessary details here but for instance it says Facebook Pixel Error: Duplicate Pixel ID:, and similar errors for other tags I use like Mixpanel, and then my JS just stops working. That's all I can say about the problems I see on my side. Best chance seems to be about looking for flagrant errors in the htaccess rules.
What should be fixed in the rules so the end goal of having the GET param page and lang work fine?
RewriteEngine On
# Part 1
RewriteCond %{THE_REQUEST} \s/+[^?]*\?lang=([^\s&]+)\s [NC]
RewriteRule ^ /%2/%1? [R=301,L,NE]
RewriteCond %{REQUEST_URI} !^/js/
RewriteRule ^([a-z]{2})(?:/([^/]+))?$ $2?lang=$1 [NC,L,QSA]
# Part 2 (this is the part I am adding, which isn't fully working well yet)
# anything looking flagrantly wrong? If for example we are on `example.com/fr/login`,
# according to rules in this htaccess file we should have 2 GET params, `lang` and `page`.
RewriteCond %{REQUEST_URI} login
RewriteRule .* index.php?page=login
# adding more pages the same way
RewriteCond %{REQUEST_URI} signup
RewriteRule .* index.php?page=signup
You can use the following rule as your Part1
RewriteEngine on
RewriteRule ^(en|fr)/login/?$ /index.php?page=login [L]
RewriteCond %{THE_REQUEST} \?lang=([^&]+)\sHTTP
RewriteRule ^ /%1%{REQUEST_URI}? [L,R]
RewriteRule ^(en|fr)/?(.*\.php)?$ /$2?lang=$1 [L]

htaccess RewriteRule - Removing '/' From The URL Causes Request To Display In URL

I have a little problem concerning .htaccess.
The problem lies more in aesthetics than functionality.
I have a very small htaccess file which contains the following, and nothing more:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9\/\-]*)$ index.php?request=$1 [QSA]
This sends any requests to index.php, parsing in the URI. I then manipulate/read the URI to get the relevant file.
An example URL would be this:
http://localhost/domain/fixtures/2015/
Now, if I was to remove the end '/', and hit enter, the URL would change to:
http://localhost/domain/fixtures/2015/?request=fixtures/2015
I have tried adding a '/' to the rewrite but it does nothing.
I have searched across Google to no avail. (Bearing in mind some of my searches went along the lines of, "/ added to url htaccess rewrite")
The main reason I want to get this sorted is because it doesn't look pretty, but also because it creates duplicate content (i.e. you can get to the same page from both URL), which is not good for SEO.
Any direction I can be pointed in is a great help.
Cheers
If I am reading this right, you want external URLs in this form:
http://localhost/domain/fixtures/2015/
And NOT this form:
http://localhost/domain/fixtures/2015
In order to achieve this, you need to use a redirect to let the remote
client know that the latter form is not valid, e.g.,
RewriteEngine On
RewriteCond %{REQUEST_URI} !/$
RewriteRule .* %{REQUEST_URI}/ [R=301,L]
If you are also serving static content, you might want this as well:
RewriteEngine On
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* %{REQUEST_URI}/ [R=301,L]
So, the full recipe including your PHP content generator:
RewriteEngine On
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* %{REQUEST_URI}/ [R=301,L]
RewriteRule ^([a-zA-Z0-9\/\-]*)$ index.php?request=$1 [QSA]

Redirecting existing URLs to SEO Friendly URLs

I have came accross a problem that every .htaccess query I've tried wasn't worked out ! I have URLs like this:
http://www.example.com/index.php?x=product
And I want to change it to a user friendly URL like this:
http://www.example.com/product/
Or it can be:
http://www.example.com/product.php
I've tried this code below:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^x=product$
RewriteRule ^index.php.*$ http://www.example.com/product.php? [R=302,L]
Now, it is redirecting perfectly, but this is not the problem. I've used this for only SEO so I must include http://www.example.com/index.php?x=product in the product.php file. Any help can be precious, thanks...
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /index\.php\?x=([^\s&]+) [NC]
RewriteRule ^ /%1/? [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/.]+)/?$ index.php?x=$1 [L,QSA]
This will redirect /index.php?x=product to /product/ and will rewrite it internally to /index.php?x=product in 2nd rule.
You don't need to put anything in the product.php file. Make sure there is a .htaccess in the directory that has the files you want to make url/seo friendly.
To have it SEO friendly make sure its in this format
http://www.example.com/product/ not http://www.example.com/product.php
if you must have a file extension, have it in http://www.example.com/products.html (you want to tell the search engine the page isn't dynamic although it is to get better pagerank)
Insert this in your .htaccess
RewriteRule /(.*)/$ products.php?x=$1
the (.*) pulls the elements out and puts them in variables $1

.htaccess rewrite to produce clean url

Currently I set a rewrite rule like so, to produce clean and simple url's.
.htaccess
RewriteRule ^/about$ about.php [L]
But what i need to do is something a little different, the other way around. For example if a user clicks the following link
about
They would go to the about page /about
Currently the about page resides at index.php?a=about&b=user and this can't be changed unfortunately. As you can see it does not look very nice in the browser address bar.
EDITED
I am using a pre-made script from phpdolphin, which is working fine. But the url are all index.php based and i would like to clean them up
Currently the only code within the .htaccess file
RewriteEngine on
RewriteCond %{request_filename} -f
RewriteRule ^(.*) $1 [L]
RewriteRule ^(([^/]*)+)(/([^/]{0,32})(/.+)?)?$ index.php?a=$1&q=$3 [L]
Add this rule before your existing rule:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?a=([^&]+)&b=user[\s&] [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteRule ^([^/.]+)/?$ index.php?a=$1&b=user [L,NC,QSA]
You can add this RewriteRule to redirect the request when user hit index.php?a=about&b=user
RewriteCond %{QUERY_STRING} ^(.*)a=about(.*)$ [NC]
RewriteRule ^index.php /about [L,NC,R=301]
or you can use php header() function in index.php to redirect the request:
if ($_REQUEST['a'] == about) {
header('Location: /about');
exit;
}
Try this i checked and it works ...
RewriteEngine On
RewriteRule ^([A-Za-z0-9-+_%*?]+)/([A-Za-z0-9-+_%*?]+)/?$ index.php?a=$1&q=$2 [L]
Note: add additional signs between [] if necessary
OR This
RewriteRule ^([^~]+)/([^~]+)/?$ index.php?a=$1&q=$2
I like to use this code because it's short and matches everything except for ~ which is very very rare to see in a url

How to rewrite dynamic URL to SEO Friendly URL?

I tried everything I see here in this site to rewrite dynamic url to SEO friendly URL.
Is it maybe because im using it in the localhost?
I try this but does not work also:
RewriteCond %{THE_REQUEST} ^GET\ /index\.php/?([^ ]*)
RewriteRule ^index\.php/?(.*) /$1 [R,L]
RewriteCond $0 !^index\.php($|/)
RewriteRule .* index.php/$0 [L]
I also refer to an online dynamic url generator , but it doesn't work either. Please help.
I would like to rewrite these couple of URLs:
index.php?p=home
index.php?p=about me
index.php?p=contact me
You can use this one:
RewriteEngine on
RewriteBase /
# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1
Then on your index.php page, all the variables will put accessible via $_GET['q']. If you further use $location=array_filter(explode('/',$_GET['q'])); then $location will be an array containing each directory, so www.mysite.com/firstdir/seconddir/thirddir will have $location[0] as 'firstdir' and $location[1] as 'seconddir' and so on. You could then compare these to url_aliases in your database to determine what content/template to display.
This also works for localhost for me, except I change the base url part from "/" to "/mycurrentconstructionsite/"
Hope that helps!

Categories