.htaccess ModREwrite - .htaccess

This is a totally new area for me so please be patient. I want to create "permalinks" for a dynamic site I am working on. At the moment all the pages (not the index) are referenced using an ID variable thus:
http://www.domainname.com/page.php?ID=122 (etc)
I want to create a suitable rewrite rule so that a useable URL would be more like this:
http://www.domainname.com/page/'pagetitle'.html (could be .php doen't matter)
Page title is stored in the database and obviously is linked directly to the ID
Am I right in thinking thr rewrite rule would be something like this?
RewriteCond %{QUERY_STRING} ^(([^&]*&)*)ID=([^&]+)(&+(.*))?$
RewriteRule ^page\.php$ /page/%3?%1%5 [L,R=301]
My ideal would be to just create
http://www.domainname.com/'pagetitle'.html
But have absolutly no idea how to do that.
Now the other question/sub question.
If the rewrite works i.e. you type in http://www.domainname.com/page/'pagetitle'.html to a browser address bar does the htaccess file work "the other way" in accessing the page http://www.domainname.com/page.php?ID=122 or do I have to create a function to take the 'pagetitle'.html bit of the URL and convert it to page.php?ID=122 ?
Also, sorry, but this is all new; if I create a site map (xml or php etc) using http://www.domainname.com/page/'pagetitle'.html will the SE spiders go to http://www.domainname.com/page.php?ID=122? or di I need to create the sitemap using the ID variables?

Question 1 and 2:
The condition is not required in this case. Use it like this:
RewriteRule ^/page/([\w-]+).html$ /page.php?title=$1 [L,R=301]
This transforms
/page/blabla.html to /page.php?title=blabla
You need to find the right page using the title parameter in page.php
Question 3:
I suggest you never use the querystring variant of the urls in any of your anchor links or xml sitemap. This way the spiders will only know of the friendly urls.

Related

.htaccess dynamic links rewrite engine

I need your help creating some links using mod_rewrite.
I have some pages like:
register.php
login.php
And have the code for them:
RewriteRule ^register/?$ register.php [NC,L]
RewriteRule ^login/?$ login.php [NC,L]
My problem is with "dynamic" links I have since I can't get them working.
For exemple I have links like:
index.php?id=news
índex.php?id=news&article=2
How can I transform those links into:
/news/
/news/article_name
And I have some products (that could have the same name in the same category) but with different ID's like:
índex.php?id=products&p=30
How can I change it to
/products/product-name
After this, is it possible to "generate" an unique name? Since I would like not set in the link the unique ID like products/45342/product-name?
What are the changes I need to make to my code to work with those links?
For example I have links like:
To clarify, you must first change the links in your application to be of the form /news/ or /news/article_name (but see below). You then rewrite these "pretty" URLs back to the underlying filesystem path.
So, to rewrite /news/ back to index.php?id=news you can do something like:
RewriteRule ^(news)/$ index.php?id=$1 [L]
Using the $1 backreference just saves typing. Only use the NC flag if this must be a case-sensitive match, but note that this potentially creates duplicate content, so you must specify the canonical URL in some other way (eg. rel="canonical" link element). For the same reason, only make the trailing slash optional if this is a specific requirement.
However, it's not possible to rewrite /news/article_name back to index.php?id=news&article=2 (I assume that should be i, and not í, as in your question?) since the article ID (ie. 2) is not present in the source URL. You need to include the ID in the source URL (or make the article_name unique and a key in your lookup). It would be more usual to create a URL like /news/2/article_name (which is what StackOverflow does), which can be easily rewritten. The article_name in the URL is purely for users (and indirect SEO). In which case you could rewrite this like so:
RewriteRule ^(news)/(\d+)/ index.php?id=$1&article=$2 [L]
This will rewrite /news/N/<anything> to /index.php?id=news&article=N (where N is 1 or more digits).
However, since it rewrites <anything> you should also implement a redirect in your application when the non-canonical article_name is accessed. (Which again, is what StackOverflow does.)
And I have some products (that could have the same name in the same category) but with different ID's like: índex.php?id=products&p=30
How can I change it to /products/product-name
The same principle as mentioned above applies here also.
After this, is possible to "generate" an unique name?
You can generate this "unique name" in your application, not .htaccess. Build you URLs in your application etc.
Since I would like not set in the link the unique ID like "products/45342/product-name" ?
As mentioned above, either your product-name is unique, and behaves like your id. Or you incorporate the unique ID in the URL - this is the far more common approach, offers greatest flexibility and is less prone to error. A "short" URL like /products/45342 will redirect you to the correct canonical URL.

Redirect dynamic url to new dynamic URL which is also prettified in htaccess

I have a website which has dynamic URLs and they're not currently prettified. I have re-worked the site and included pretty URLs, but the real dynamic folder structure/filename has also changed.
To better explain, the current dynamic URLs look like
http://example.com/liveguide/year.php?year=2017
The new dynamic url for the same page is
http://example.com/shows/show-list.php?year=2017
I use the following:
RewriteRule ^shows/(199[5-9]|200[0-9]|20[0-1][0-7])/?$ /shows/show-list.php?year=$1 [L]
To enable the use of pretty URLS like
http://example.com/shows/2017
So what I'm trying to do is if anyone followed a link of the original dynamic URL, they'll end up on the new clean URL. So far I've just got
RewriteRule ^liveguide/year.php /shows/show-list.php [R=301,L]
Which redirects to the correct page, but you're left with the ugly URL in the address bar. How could I do it so that the new, pretty URL is in the address bar?
Ie someone visits
http://example.com/liveguide/year.php?year=2017
They end up on, and see in their address bar
http://example.com/shows/2017
You just need to match the query string, which is in a separate variable, and used in a RewriteCond, which capture to %1, %2 etc. Like this:
RewriteCond %{QUERY_STRING} ^year=(\d{4})$
RewriteRule ^liveguide/year\.php$ /shows/%1? [R=301,L]
While we are here, do you really need to only match just those exact years? Would it matter if /shows/1234 also got rewritten? Probably not, you can just return a 404 from your PHP, so a simpler rule would be ok, like the above just saying any four numbers. It will also work for future years without changing it.
RewriteRule ^shows/(\d{4})/?$ /shows/show-list.php?year=$1 [L]
The goal is usually not to match exactly what you want, and only that, but rather to ensure nothing else (other parts of the site) will be matched that shouldn't be. Simpler rules are easier to maintain and review later. Your script must already be able to handle bad data anyway, so just let it handle the detailed checking without duplicating it unnecessarily.
Hope this helps.

.htaccess rewrite url that has already been changed

I am upgrading my site which involves new scripts and a different URL
structure. There are currently a few thousand pages so I want to
basically move them in to a subdirectory so that they are not lost.
I am not very confident with htaccess so can someone please confirm that
the first part I have done is correct:
Old URL: http://www.example.com/article/another-dir/page-group/whatever.html
RewriteRule ^article/?$ http://www.example.com/archive/ [R=301,NC,L]
To achieve this: http://www.example.com/archive/another-dir/page-group/whatever.html
Search engines will see the above as a permanent move and in the address bar
it will show the new url. Is this right?
The second part is on the new script - the url's could be improved but I am
unable to change all the script so thought about using htaccess again but am
not sure if it can be achieved like this.
At the moment the url looks like this:
url: http://www.example.com/category/4/categoryname
In the htaccess the current rewrite rule for this type of url looks like this:
RewriteRule ^category/(.*)/(.*)$ category.php?id=$1&slug=$2
Is it possible to change this so that in the url address bar I end up
with this:
http://www.example.com/categoryname
Basically I don't want to see either the number or category/ in the resulting
url as it will be easier for visitors to use - is that possible??
Thanks in advance for any help.
The second question related to passing in URI components for querystring redirect and then hiding those components in the URL I don't think would be easy, if even possible, using RewriteRules.
For the first question though. Given the sample URLs you mentioned, the RewriteRule would need to include capture and backreference if you want to preserve the full URL in the redirection. For example:
RewriteRule ^article/?(.*)$ http://www.example.com/archive/$1 [R=301,NC,L]

GET parameter becomes the page name after rewrite

I have an url that looks like:
/platforms.php?platform_id=xxx
where xxx is a number
I'm rewriting the URL inside the php application. So, for example the above url would look like:
/xbox/ or /playstation/
Now in .htaccess I have:
RewriteRule ^([^/]+)/$ platforms.php?platform_id=$1 [L,QSA]
However when I go to a platform page the GET url becomes /xbox/ or /playstation/ , instead of xxx.
Any pointers would be appreciated.
Update:
Hi, the link is not relevant to my question. I've tried to reformulate what I am after for in the example bellow with better details.
Thanks for the answer and sorry for the bad explanation.
Yep, when I said GET url I was referring to $_GET["platform_id"] .
Basically I have an URL called
www.example.com/platforms.php?platform_id=1
In the above example $_GET['platform_id'] = 1.
In the actual php aplication I have a function (let's call it make_link ), with which I make the above URL output like:
www.example.com/xbox/ (since 1 is the id of the xbox platform)
Now in httaccess I also need a rewrite rule that will make accessing the URL work.
So I have :
RewriteRule ^([^/]+)/$ platforms.php?platform_id=$1 [L,QSA]
This does make the rewrite work in the terms that I can access
www.example.com/xbox/
However on the newly accessed page, if I get $_GET['platform_id'], the value for it is xbox/ .
Thanks,
In a RewriteRule, $1 is a variable backreferencing the first regular expression (in your case: ([^/]+)).
So, whatever text forms that part of your URL is what will be stored in $1.
If you wanted to use the consoles' IDs, you'd have to make these IDs part of your URLs. If you don't want that, but if you want your pretty URLs to reflect the names of the consoles, you'll have to rewrite the query part of platforms.php?platform_id=$1 in your .htaccess file.
Instead of querying for IDs (?platform_id=$1), you'll have to query for the consoles' names, e.g. ?platform_name=$1.
Edit:
In your PHP file, you'd then use $_GET['platform_name']

Complex htaccess URL and variable rewriting

I run a mmo game fan site http://www.ddmsrealm.com. In this site I have run a databse with quest and item information for years. It was originally built with MSSQL/ASP and I have recently converted it to MYSQL/PHP. In this conversion I optimized the database. In doing so I changed a bit of the structure and variables. Now, the new pages are up and running but I am having a terrible time trying to write rules in my htaccess file to accommodate the hundreds of quests and thousands of items to reroute to the new database pages with the new database variables. Any and all help would be appreciated as I struggle to learn apache/htaccess.
I have two “details” pages that need to be rerouted along with having the variable names changed. The actual ID's are still the same so it should work.
Original Quest Page was an asp page:
~http://www.ddmsrealm.com/ddo/quests/QDetail.aspx
Permanent reroute to new Quest Details Page(WordPress Template Page):
~http://www.ddmsrealm.com/index.php/dungeons-and-dragons-quest-and-magic-item-database/dungeons-and-dragons-online-quest-info
Now the parameters need to be rewritten like this:
Old: QuestID to New: ddoQuestID
Old: SeriesID to New: ddoSeriesID
So the idea is that this:
~http://www.ddmsrealm.com/ddo/quests/QDetail.aspx?QuestID=210&SeriesID=30
Is permanently redirected as this:
~http://www.ddmsrealm.com/index.php/dungeons-and-dragons-quest-and-magic-item-database/dungeons-and-dragons-online-quest-info?ddoQuestID=210&ddoSeriesID=30
If I can get the formula and steps for this I am sure I can apply it to the other pages. Being new to htaccess rewriting I am struggling to understand what exactly needs to happen and in what order. As you can imagine, this has jacked up my SEO bad having about 5000+ pages now going to a 404. Plus hundreds of websites have linked that old URL and now those links are trashed until I can resolve this.
Thank you so much in advance for the help figuring this out!
The URI rewrite itself is pretty straight forward using a RewriteRule but changing the query string you'll need to use a RewriteCond and match against the %{QUERY_STRING} variable and backreference the matches using the % symbol. See the docs for more info: http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html
Try something like this:
RewriteEngine On
# Match the query string
RewriteCond %{QUERY_STRING} QuestID=([0-9]+)&SeriesID=([0-9]+)
# Rewrite the URI and append the new query string
RewriteRule ^ddo/quests/QDetail.aspx$ /index.php/dungeons-and-dragons-quest-and-magic-item-database/dungeons-and-dragons-online-quest-info?ddoQuestID=%1&ddoSeriesID=%2 [R=301,L]

Resources