Rewrite URL Structure - .htaccess

I would like to change the URL structure of my site. My current URL structure is like this:
www.domain.com/events/events.php?location=san%20francisco
I want to change the URL structure to:
www.domain.com/events/san-francisco
My XMPL site map has the URLs listed like the first example. I want the search engines to index the URL like the second example. What are all the things I need to do to achieve this? The {city} in the location parameter is dynamic depending on the user's IP address. Do I need to change my XML sitemap to list new URL structure? What do I need to put in my .htaccess file. Do I need to change the actual URLs links on my website to the new structure or can I just use .htaccess?

This should work:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /events\.php\?location=(.*)%20(.*)\ HTTP
RewriteRule ^ /events/%2-%3\? [R=302,L]
RewriteRule ^events/(.*)$ /events.php?location=$1 [L]
Changing R=302 to R=301 when you know it redirect correctly
EDIT:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /(.*)/(.*)\.php\?location=(.*)%20(.*)&lid=(.*)&slid=(.*)\ HTTP
RewriteRule ^ /%3/%4-%5/%6/%7\? [R=302,L]
RewriteRule ^(.*)/(.*)/(.*)/(.*)$ /$1/$1.php?location=$2&lid=$3&slid=$4 [L]
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /(.*)/(.*)\.php\?location=(.*)%20(.*)\ HTTP
RewriteRule ^ /%3/%4-%5\? [R=302,L]
RewriteRule ^([A-Za-z0-9]+)/([A-Za-z0-9]+)$ /$1/$1.php?location=$2 [L]

You have to use a .htaccess file similar to the one below. It will match fancy URL and forward the request to the right php file.
RewriteEngine On
RewriteRule ^([^/]*)/(.*)$ /$1/$1.php?location=$2
With this, both www.domain.com/events/events.php?location=san%20francisco and www.domain.com/events/san-francisco will lead to the same page.
But if you don't change your sitemap nor the links on your site, search engines won't know that.
You have then 2 solutions :
change all your links in your site and sitemap with the new structure URL
use a redirect rule to redirect "old" pages to the "new" ones
From what I know about SEO, the first option is the best. Or even better, a combination of both solutions : change all your links, and use a redirect 301 rule so pages already indexed by search engines will not lost their ranking and to avoid duplicate content.
RewriteRule ^([^/]*)/(.*).php?location=(.*)$ http://www.yourdomain.com/$1/$3 [R=301]

Related

hard time with rewriting dynamic url

I'm trying to convert the dynamic urls (with query string) of my website to friendly ones. That's the first time for me and I'm doing it for seo optimization.
I reached the point where if I type the friendly url, the browser sends me to the equivalent dinamic url (and I see the friendly url in the navigation bar), but if I navigate inside the website the navigation bar still shows me the url with the query.
Is that right?
Here's my .htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example\.it
RewriteRule ^(.*)$ http://www.example.it/$1
RewriteRule (.*).html$ /$1.php [R=302]
RewriteRule ^ita/([A-Za-z0-9\-\+]+).php$ $1.php?lang=it [NC,L]
RewriteRule ^eng/([A-Za-z0-9\-\+]+).php$ $1.php?lang=en [NC,L]
RewriteRule ^ita/kiwishop/([A-Za-z0-9\-\+]+).php$ kiwishop.php?shop=$1&lang=it [NC,L]
RewriteRule ^eng/kiwishop/([A-Za-z0-9\-\+]+).php$ kiwishop.php?shop=$1&lang=en [NC,L]
RewriteRule ^sitemap.xml ./sitemap.php [L]
Should I also do the opposite operation (I mean set a rewriterule that rewrite the dinamic url into the friendly one)? If so, how can I do that?
Another question: which url should I put in the sitemap.xml, the friendly one or the one with the query?
You need to change all links on your site to the friendly URLs.
You should put in the sitemap whatever URL you want to show in search engines. It sounds like you want that to be the friendly URL.

How to rewrite url for certain pages in htaccess?

I am trying to show different url and redirect users to specific url with .htcaccess when they click on a blog post but to no avail.
Lets say the url is: http://localhost/mySite/article.php?article_title=test-title
then I would like to show it as http://localhost/mySite/article/test-title
This is my current htcaccess file:
#turn on url rewriting
RewriteEngine on
#remove the need for .php extention
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
#rewrite rule for blog
RewriteRule article/([A-Za-z0-9-]+) /mySite/article.php?article_title=$1
But for some reason it is not redirecting/showing the correct url. I am not getting any errors.
EDIT
Trying to ask my question again and explain it better. Let's say the url is
http://localhost/www.example.com/admin/editUser.php?user_id=126
and I would like to rewrite the url like this:
http://localhost/www.example.com/admin/user/126
then how can I achieve this. I tried using this website to check the modified url but it does not work. Seems like it does not work with any of the accepted answers here in stack at all.
This is my htaccess file atm. It is in the root of www.example.com
#turn on url rewriting
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^user/([0-9]+)/?$ /editUser.php?user_id=$1 [NC,L] # Handle user edit requests
Apache Module mod_rewrite is enabled. Also added an alias. Still no changes in the url. If I try something really basic like this:
# redirect to .php-less link if requested directly
RewriteCond %{THE_REQUEST} ^[A-Z]+\s.+\.php\sHTTP/.+
RewriteRule ^(.+)\.php $1 [R=301,L]
it works fine.
Why is the users redirect not working? What am I doing wrong.
Try it like this for your rule for article url in mysite directory.
RewriteRule ^article/([A-Za-z0-9-]+)$ article.php?article_title=$1 [QSA,NC,L]
you need to mention start ^ and end $ of string.

ASCII Characters in Links Producing 404 Errors

I used to have a query string ?q= in all my urls. I now have these urls redirecting to the alias name.
RewriteRule ^(.*)$ index.php?q=$1 [L,NC]
RewriteCond %{QUERY_STRING} ^q=(.*)$ [NC]
RewriteRule ^$(.*) /%1? [R=301,L,NE]
Unfortunately other websites link to pages on my website in this manner www.example.com/%3Fq%3Dfaqs. I changed over all the links on my site to absolute but somehow google has indexed hundreds of pages that look like www.example.com/%3Fq%3Dfaqs?q=contact or www.example.com/%3Fq%3Dfaqs%3Fq%3Dcontact.
Is there a way I can rewrite these ASCII codes back to the symbols using htaccess or some other method?
I solved this by writing lines like Redirect 301 /?q=faqs example.com/?q=faqs This redirected /%3Fq%3Dfaqs to /?q=faqs which redirected to /faqs. Probably not the best way to do it, but it works.

How do I rewrite the url?

Could someone tell me how to rewrite this URL. I have looked at a lot of questions on stackoverflow but they seem to be missing my answer.
RewriteEngine On
That is what I have... its a bit poor.
I need to rewrite url's if they do not point to a directory.
I need to do this...
any.domain.com/pages/some-page-slug/login
To be rewritten to the correct url of...
any.domain.com/pages/login.php?page=32
Does anyone have any ideas on how this can be achieved?
1) Rewriting product.php?id=12 to product-12.html
It is a simple redirection in which .php extension is hidden from the browser’s address bar and dynamic url (containing “?” character) is converted into a static URL.
RewriteEngine on
RewriteRule ^product-([0-9]+)\.html$ product.php?id=$1
2) Rewriting product.php?id=12 to product/ipod-nano/12.html
SEO expert always suggest to display the main keyword in the URL. In the following URL rewriting technique you can display the name of the product in URL.
RewriteEngine on
RewriteRule ^product/([a-zA-Z0-9_-]+)/([0-9]+)\.html$ product.php?id=$2
3) Redirecting non www URL to www URL
If you type yahoo.com in browser it will be redirected to www.yahoo.com. If you want to do same with your website then put the following code to .htaccess file. What is benefit of this kind of redirection?? Please check the post about SEO friendly redirect (301) redirect in php and .htaccess.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^optimaxwebsolutions\.com$
RewriteRule (.*) http://www.optimaxwebsolutions.com/$1 [R=301,L]
4) Rewriting yoursite.com/user.php?username=xyz to yoursite.com/xyz
Have you checked zorpia.com.If you type http://zorpia.com/roshanbh233 in browser you can see my profile over there. If you want to do the same kind of redirection i.e http://yoursite.com/xyz to http://yoursite.com/user.php?username=xyz then you can add the following code to the .htaccess file.
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?username=$1
5) Redirecting the domain to a new subfolder of inside public_html.
Suppose the you’ve redeveloped your site and all the new development reside inside the “new” folder of inside root folder.Then the new development of the website can be accessed like “test.com/new”. Now moving these files to the root folder can be a hectic process so you can create the following code inside the .htaccess file and place it under the root folder of the website. In result, www.test.com point out to the files inside “new” folder.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^test\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.test\.com$
RewriteCond %{REQUEST_URI} !^/new/
RewriteRule (.*) /new/$1
TO do this you need to write a front controller.
See here, here, here, and here.
Alternatively in Apache you can rewrite this
any.domain.com/pages/32/login
or this:
any.domain.com/32/login
or even this:
any.domain.com/some-slug/32/login
to this:
any.domain.com/pages/login.php?page=32
One way or another to do this with only apache you need to supply the page id in some fashion. Keep in mind even with format any.domain.com/some-slug/32/login the content of the slug is irrelevant and won't necessarily link to the correct page. Which I imagine is undesirable and bad for SEO.
Another alternative is using RewriteMap. But this will be tricky and require reloading apache configurations whenever a page/slug is created/edit.
I understand that pages and login are static in this case and some-page-slug is changing. And you always want to redirect to static page /pages/login.php?page=32
So this is how to do it:
1) Rewrite
RewriteEngine On
RewriteRule ^pages/([a-zA-Z0-9_]+)/login(.*)$ /pages/login.php?page=32
or 2) Redirect Pernament
RewriteEngine On
RewriteRule ^pages/([a-zA-Z0-9_]+)/login(.*)$ /pages/login.php?page=32 [R=301,L]
or 3) Redirect Temporary
RewriteEngine On
RewriteRule ^pages/([a-zA-Z0-9_]+)/login(.*)$ /pages/login.php?page=32 [R=302,L]
Here is great article about htaccess trics
http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/

htaccess redirect for subdomains -> similarly-named subdirectories?

I'm restructuring a web site with a great deal of content currently parked at URLs that look like this.
http://string.domain.com/year/month/dd/string-pulled-from-title
For various reasons, I'd like to park all new content at URLs that looks like this
http://www.domain.com/blogs/string/year/month/dd/string-pulled-from-title
I'd like to make the change for future content, but don't want all the old stuff to go 404.
I believe a 301 redirect rule in my htaccess will do the trick, sending all referred traffic coming in through old links to the new formats.
But what should this rule look like? I've read a few tutorials but haven't found this exact case in any examples.
Note, I don't want to do this for all subdomains, only for about 10 specific ones. So if someone could help me figure out one of these, then I can copy paste it 10 times in my htaccess for each subdomain and be set.
Drop this into the .htaccess file of the old site (adjusting the domain to your actual one):
RewriteEngine On
RewriteRule ^(.*)$ http://example.com/blogs/string/$1 [R=301]
This will grab this part of the URL at the old site:
year/month/dd/string-pulled-from-title
and redirect it to the new site under the new location:
blogs/string/year/month/dd/string-pulled-from-title
Alternatively, if you want something a little more variable like, without having to custom fix each .htaccess, drop this in the file for each subdomain instead:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*).example.com
RewriteRule ^(.*)$ http://example.com/blogs/%1/$1 [R=301,L]
If you're redirecting to the same domain, and it includes the www, adjust the rewrite rules to the following:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*).example.com
RewriteCond %{HTTP_HOST} !^www.example.com [NC]
RewriteRule ^(.*)$ http://example.com/blogs/%1/$1 [R=301,L]
Note the second RewriteCond which checks to make sure that the URL requested does not include the leading www, which may lead to an endless redirect if the destination URL itself includes www and would try and redirect that subdomain as well.
%1 grabs the first capture group from the line above.
$1 references the first capture group on the same line.

Resources