HTAccess Redirect using main parameter, ignore all others - .htaccess

firstly I know and understand how to redirect based on parameters :)
My issue is that I need to redirect all links based on the supplied MenuID parameter and ignore any other information in the query string, as not all parameters are used in each web page request, e.g. menuid=2738421; is New Products
http://www.domain.com/shop.php?menuid=2738421&menuref=Ja&menutitle=New+Products& limit=5&page=24
or,
http://www.domain.com/shop.php?menuid=2738421&menuref=Ja&menutitle=New+Products&limit=20&page=3
or,
http://www.domain.com/shop.php?menuid=2738421&menuref=Ja&page=12&limit=15
to
http://www.domain.com/new.html?page=x&limit=x
The reason for the redirection is that search-engines have indexed these pages and so I need to prettify the URLs.
Is this actually possible to create a fuzzy redirect criteria?
## 301 Redirects
# 301 Redirect 1 - works for this explicit URL, but need a partial result
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^new\.html$ http://www.monarchycatering.com/shop.php?menuid=2738421&menuref=Ja&menutitle=New+Products&limit=5&page=24 [R=301,NE,NC,L]
Any help gratefully taken, thank you in advance
Mark.

Sorry for the delay, but StackOverflow doesn't seem to have a way to flag answers that have been replied to and need my attention.
OK, if I understand you correctly, you have an incoming "reduced" semi-SEF URL out in the real world (produced by your links), such as
http://www.domain.com/new.html&limit=5&page=24
("real" SEF would be something like http://www.domain.com/new/limit/5/page/24.html)
and you need to use .htaccess to map it to real files and more Query String information:
http://www.domain.com/shop.php?menuid=2738421&menuref=Ja&menutitle=New+Products&limit=5&page=24
You want to detect new.html for example, and replace it by a fixed string shop.php?menuid=2738421&menuref=Ja&menutitle=New+Products&, using the [QSA] flag to append the existing Query String on to the end?
RewriteEngine On
RewriteRule ^new\.html /shop.php?menuid=2738421&menuref=Ja&menutitle=New+Products [QSA]
RewriteRule ^sale\.html /shop.php?menuid=32424&menuref=Ja&menutitle=Products+On+Sale [QSA]
...etc...
I believe that a & will be stuck on the end of the rewritten address if the user supplied a non-empty Query String, but be sure to test it both ways.
P.S. It probably would have been cleaner to use "comment" to reply to my question, rather than adding another answer.

It's not clear to me what your starting point is and where you're trying to end up. Do you have "pretty" URLs that you want to convert into "non-pretty" Query Strings that your scripts can actually digest?
The reason for the redirection is that search-engines have indexed
these pages and so I need to prettify the URLs.
If the search engines have already indexed the Query String non-pretty version, they'll have to now re-index with pretty URLs. Ditto for all your customers' bookmarks.
If you want to generate "pretty" links within your site, and decode them (in .htaccess) upon re-entry to non-pretty Query Strings, that should work. Your customers' existing bookmarks should even continue to work, while the search engines will replace the non-pretty with the pretty URLs.

and thanks for the interest in my question...
I have rewritten parts of my website and Google still has references to the old MenuID parameter and shop.php configuration, but now I rewriten the Query to a prettier format, e.g.
http://www.domain.com/shop.php?menuid=2738421&menuref=Ja&menutitle=New+Products&limit=5&page=24
is now
http://www.domain.com/new.html&limit=5&page=24
The pages represent product categories, and so needed to be displayed in a more meaningful manner. Customer bookmarking is not an issue, as long as I can redirect the pages.
I hope that makes sense, best wishes,
Mark.

Related

htaccess redirect pretty URLs to ugly ones

So, I'm trying to make my URL's a bit more pretty and sharable. I have a website with some items that users can currently access with example.com/?i=itemName. However, I'd like users to be able to write example.com/itemName instead.
This means I'd have to do some redirection with htaccess. I want to redirect all URL's to example.com itself, but keep the URL the same. To clarify, an example:
User types example.com/niceItem. The server shows the content of example.com, but keeps the URL as example.com/niceItem (alternatively, it can change the URL to example.com/?i=niceItem, then I can simply read the URL with javascript and change it back to example.com/niceItem in the adress bar).
So far, this is the best I could do:
RewriteRule ^/([^\/]+)$ /index.php?i=$1 [NC,L]
The idea is to capture the requests that don't have slashes after the first one (like example.com/niceItem), and then read the file at example.com/index.php?i=niceItem. The problem is, when I load a page like example.com/niceItem, the page displays what the value of i is with php; it should be niceItem, as the link is supposed to be example.com/?i=niceItem, but the value of i is actually the string index.php. Not quite what I wanted. Also, I'd expect the following to work
RewriteRule ^/([^\/]+)$ /?i=$1 [NC,L]
but this actually causes an internal server error.
So, the question is, why do those not work, and how would I be able to achieve what I'm trying to achieve?
PS. Actually, this website I'm talking about is a subdomain of example.com. So, I have sub.example.com which maps to example.com/sub/, and I need the URL's to be prettyfied like sub.example.com/itemName or example.com/sub/itemName. As I mentioned, the format of the URL isn't that big of a deal as long as the itemName part is in there. I'll be able to read the URL with javascript and change it to whatever I want once the page has loaded.
Use RewriteCond
If i is the only query argument that will be passed then
RewriteCond "%{QUERY_STRING}" "(\?i=)(.*)$"
RewriteRule "(.*)/?$" "$1/%2"
If you need to extract i only but keep other query args
RewriteCond "%{QUERY_STRING}" "(.*(?:^|&))i=([^&]*)&?(.*)&?$"
RewriteRule "(.*)/?$" "$1/%2?%1%3"
Most every framework provides this sort functionality. It is best not to reinvent the wheel when possible. This is a fragile setup, and it will probably cause you headaches in the future.

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 redirect to subfolder, and remove it's name

I'm kind of noob in the world of web so my apologies... I tried many things found on SO and elsewhere, but I didn't manage to do what I want. And the Apache documentation is... well too much complete.
Basically what I want to do is redirect my domain to a subfolder. I found easy solutions for this (many different actually).
http://www.foo.com/
http://foo.com/
should redirect to /bar and appear as http://foo.com/
Using the following I got the expected result :
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^www\.foo.com$
RewriteRule ^/?$ "http\:\/\/foo.com" [R=301,L]
RewriteRule ^((?!bar/).*)$ bar/$1 [NC,L]
But I also want the subfolder as well as filenames not to appear when explicitly entered, i.e :
http://www.foo.com/index.html
http://foo.com/index.html
http://wwww.foo.com/bar
http://foo.com/bar
http://wwww.foo.com/bar/index.html
http://foo.com/bar/index.html
Should all appear as
http://foo.com/
Is this possible ?
Obviously using .htaccess, since I'm on a virtual host.
Thanks
As Felipe says, it's not really possible, because you lose information when you do that R=301 redirect: a hard redirect like this starts a whole new request, with no memory of the previous request.
Of course, there are ways to do similar things. The easiest is to put the original request in the query string (here's a good rundown on how mod_rewrite works with query strings). Sure, the query string does show up in the URL, but most modern browsers hide the query string in the address bar, so if your goal is aesthethics, then this method would be workable.
If you really don't want to show any of the original query in the URL, you might use cookies by employing the CO flag (here are some very good examples about cookie manipulation). At any rate, the information about the original request must somehow be passed in the hard redirect.
But anyhow, and most importantly, why would you want to do something like this? It's bound to confuse humans and robots alike. Great many pages behaved like this back when frames were fashionable, and it was pretty terrible (no bookmarking, no easy linking to content, Google results with the snippet "your browser cannot handle frames", no reloading, erratic back button, oh boy, those were the days).
Speaking of which, if your content is html, you may just use a plain old iframe to achieve the effect (but I'd sincerely advise against it).

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]

using mod_rewrite to create SEO friendly URLS

I've been searching google for this but can't find the solution to my exact needs. Basically I've already got my URL's named how I like them i.e. "http://mysite.com/blog/page1.php"
What I'm trying to achieve (if it's possible!) is to use rewrite to alter the existing URLS to: "http://mysite.com/blog/page1"
The problem I've come across is I've found examples that will do this if the user enters "http://mysite.com/blog/page1" into the broweser which is great, however I need it to work for the existing links in google as not to loose traffic, so incoming URLS "http://mysite.com/blog/page1.php" are directed to "http://mysite.com/blog/page1".
The 1st example (Canonical URLs) at the following is pretty much what you want:
http://httpd.apache.org/docs/2.0/misc/rewriteguide.html#url
This should do the trick, rewriting requests without .php to have it, invisible to the user.
RewriteEngine On
RewriteRule ^/blog/([^.]+)$ /blog/$1.php
You will need to write a rewrite rule for mapping your old url's to your new url as a permanent redirect. This will let the search engine know that the new, seo friendly url's are the ones to be used.
RewriteRule blog/page1.php blog/page1 [R=301,L]

Resources