I am trying to do some URL rewriting with Apache and PHP. This is for SEO reasons so that foo.com/product-category/product-title becomes foo.com/product.php?id=999. I would of course want the URL not to change and be the SEO friendly version.
I have a current site which has a working setup but it's very much a hack rather than a maintainable solution, so I am trying to do things right!
At the moment I have:-
RewriteRule ^([a-zA-Z0-9_\ \-\%]+)/?($) /url_redirect.php?page=$1 [QSA,L]
Which works perfectly, http://foo.com/random-url?foo=1&bar=2 ends up at /url_redirect.php with $_GET['page'] = 'random-url' and $_GET['foo'] = 1 etc etc..
I want the exactly the same thing to happen if you one level deeper
(eg http://foo.com/non-existing-directory/random-url?foo=1&bar=2)
I could make the non-existing-directoy a real directory and then .htaccess from there but there has to be a easer way.. I would prefer me not to have to hardcode any directory names (which is sort of how my current solution works).
I think the issue is probably something not right with the regular expression, they always defeat me.
I just tried this, might not be the best way to achieve it though (I'm no rewrite guru)
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_\ \-\%]+)/([a-zA-Z0-9_\ \-\%]+)?($) url_redirect.php?page=$2 [QSA,L]
RewriteRule ^([a-zA-Z0-9_\ \-\%]+)/?($) url_redirect.php?page=$1 [QSA,L]
Sorry that was for my local setup here's how I think you would want it!
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_\ \-\%]+)/([a-zA-Z0-9_\ \-\%]+)?($) /url_redirect.php?page=$2 [QSA,L]
RewriteRule ^([a-zA-Z0-9_\ \-\%]+)/?($) /url_redirect.php?page=$1 [QSA,L]
Notice the / before url_redirect.php
Related
I think this has been discussed over here in past but i don't know exactly what i have to search for so please if you could help just give me a hand.
Well i want to create a url rewrite with multiple options. I cannot explain here with few so i would rather give a simple example.
A url:
example.com/info.php?id=1
example.com/info.php?id=1&edit
I want to rewrite to like this
example.com/info/1
example.com/info/1/edit
I think you want the opposite: example.com/info/1 -> example.com/info.php?id=1
RewriteEngine on
RewriteRule ^info/([^/]+)/edit/?$ info.php?id=$1&edit [L]
RewriteRule ^info/([^/]+)/?$ info.php?id=$1 [L]
I have searched for a solution of this problem all over the Internet, read various threads and articles about it but did come to a full solution for my - i think quite generic problem in mod_rewrite. Here is the issue:
I have developed a small webapp that lets users make calculations (splitting costs after holidays "who pays to whom"). Now I have come to the point where I want to make it (especially the static pages) language dependent. What seemed like no big deal by passing a get parameter ?lang= seems to be a problem for search engines according to my research - so apache mod_rewrite to the rescue to have beautiful URLs like
example.com/en/index => example.com/index.php?lang=en.
example.com/en/about => example.com/about.php?lang=en.
Moreover, users should be able to share their calculations with their friends - for this they are issued an ID after caluclation therefore a rule like
example.com/c/9842398dfalkjdsf98sfdasf => example.com/c.php?id=9842398dfalkjdsf98sfdasf
is used to call their previous calculations (language handling is done here directly in the script automatically, also this links are not needed to be indexed in any search engine).
To achieve this I have come up with these rules:
RewriteEngine on
RewriteRule ^c/([^/]+) c.php?id=$1 [L,QSA]
RewriteRule ^c/([^/]+)/ c.php?id=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/$ $2.php?lang=$1 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)$ $2.php?lang=$1 [L,QSA]
So far these rules work - however:
Here are my questions:
1) Is this approach a good approach for language dependent sites - meaning will google index the "static" sites like "about" etc. correctly?
2) Come somebody come up with a Rewrite Rule to also have requests like
example.com/about => example.com/about.php?lang=en.
(notice the missing language parameter in the first url)
to send them to the standard language
OR should I then first get their accpted langauge and then redirect them to example.com/LANG/about
3) How should I design the language detection - especially on the homepage? Right now it works according to the rules above - howver I have seen solutions on the Internet passing everything first to a index.php which then call the disred page like
index.php?lang=en&page=about
When google "visits" it will usually not provide an HTTP_ACCEPT_LANGUAGE so will it even ever see the other language versions like example.com/it/about ?
4) Turns out that using RewriteRules kill your relative CSS, JS, picture links in your code (suprise!), however I found a page on the internet saying that this also could be handled with a RewriteRule instead of using absoulute paths in the html? (here). Unfortunately I where not able to implement it.
In a nutshell:
I am a little confused, hope somebody can help how to set up a simple SEO conform, language dependent site and that this will help others to see a best practice solution as a whole.
Thanks in advance!
Try this , thank you
RewriteEngine On
# This is to prevent the rules from looping, they only work as on-shot
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
# If the url is blank, go to 'web/en/'
RewriteRule ^/?$ /web/?lang=en [L,QSA]
# If the url starts with en,es,pt-br, remove it and add ?lang=$1 ,has /web
RewriteRule ^/?(en|es|pt-br)/web(/?.*)$ /web$2/?lang=$1 [L,QSA,R]
# If the url starts with en,es,pt-br, remove it and add ?lang=$1 ,has no /web
RewriteRule ^/?(en|es|pt-br)/?$ /web/?lang=$1 [L,QSA,R]
# If the url starts with en,es,pt-br, remove it and add ?lang=$1 ,everything else
RewriteRule ^/?(en|es|pt-br)/(.+?)/?$ /$2/?lang=$1 [L,QSA,R]
So I want to use mod_rewrite to transform:
random_name.com/main.php?user=$user
into
random_name.com/$user
(the $user is a php variable added onto the url, so it can be any name such as andy or rebecca, names like that)
and my code for the .htaccess is:
RewriteEngine on
RewriteRule ^/([A-Za-z0-9-]+)/?$ main.php?id=$1 [NC,L]
But this doesn't seem to work for some reason. I've read up on the tutorials but they're really complicated and it seems like this would do the trick, but it doesn't. I'll appreciate it if anyone who has experience with mod_rewrite would give me a few pointers.
You cannot use mod rewrite to transform random_name.com/main.php?user=$user into random_name.com/$user.
You have to do it manually in all the links on your site.
After that you may use mod rewrite for the reverse transformation, which will make /main.php?user=$user out of /user request
Try this:
RewriteEngine on
RewriteRule ^([A-Za-z0-9-]+)$ main.php?user=$1 [NC,L]
i'm new to mod_rewrite, and i'm trying to convert my web address from:
website.com/profile.php?user=andy
to the following:
website.com/user/andy
This is my following code:
RewriteEngine On
RewriteRule ^user/([A-Za-z0-9]+)/?$ profile.php?user=$1 [NC,L]
I researched extensively and this does seem to be the correct way to do it, but it doesn't redirect to where i want it to, it just redirects to this:
http://website.com/profile.php?user=andy
which means i must doing something wrong...
Can anyone help me out here? I would appreciate any tips.
If you want
http://website.com/profile.php?user=andy ->301-> http://website.com/user/andy
http://website.com/user/andy means http://website.com/profile.php?user=andy
They are 2 different things, you'll need 2 rules:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^user=([A-Za-z0-9]+)
RewriteRule ^profile.php /user/%1? [R=301,L]
RewriteRule ^user/([A-Za-z0-9]+)/?$ profile.php?a=b&user=$1 [L]
The first will 301 (moved permanently) redirect to the pretty url.
The second will allow your application to understand the pretty url.
Whenever you change the url scheme for a site you should take care of existing links. As such, that first rule is required/a good idea. You should not, however, need the first rule when using your own application. If your own application is generating links to profile.php?user=me - change your application code.
You have to change your URLs when outputting them in your HTML to be in the format you want (/user/andy).
mod_rewrite will rewrite /user/andy to main.php?... not the other way around.
What do you mean by my result?
mod_rewrite won't change existing links in your source code. Navigate to website.com/user/andy and you should see it work.
So here's the deal:
episode.php?s=1&e=3 brings up information for season 1 episode 3.
So make it SEO friendly I want to be able to have users be able go to the following:
domain.com/episodes/ShowName_Season_1_Episode_3_The_Title_Of_The_Episode.html (or without the .html, doesn't really matter).
This is what I have so far:
RewriteRule ^episodes/([a-z0-9-]+)_$_([a-z0-9-]+)_$_([a-z0-9-]+).html episode.php?s=$1&e=$2
I know this is not right...how can I make it take the two numbers in the rewritten URL and associate them with PHP GET variables in the actual URL?
I know how to do more basic mod_rewrites but this one has been confusing me. I am guessing it is something stupid, so thanks in advance for your patience.
Here's what you can do:
RewriteEngine ON
#domain.com/episodes/ShowName_Season_1_Episode_3_The_Title_Of_The_Episode.html
RewriteRule ^episodes/([a-z0-9\-]+)_Season_([0-9]+)_Episode_([0-9]+)_(.*)$ episode.php?s=$2&e=$3 [NC,L]
#Even more SEO friendly: http://domain.com/ShowName/Season/3/episode/4/title/
RewriteRule ^episodes/([a-z0-9\-]+)/season/([0-9]+)/episode/([0-9]+)/(.*)/? episode.php?s=$2&e=$3 [NC,L]
#or Even more SEO friendly: http://domain.com/episodes/ShowName/season-3/episode-1/title
RewriteRule ^episodes/([a-z0-9\-]+)/season\-([0-9]+)/episode\-([0-9]+)/(.*)/? episode.php?s=$2&e=$3 [NC,L]
I don't recommend to use "_" for SEO but "-"