help regarding dynamic redirect rule in htaccess - .htaccess

I need ur help for given subject.
I am playing with htaccess rules first time in life.
here is the scene -
i want to redirect the urls -
http://www.abc.com/var1
http://www.abc.com/var2
to follwing urls -
http://www.abc.com/index.php?u=var1
http://www.abc.com/index.php?u=var2
In this case the values var1 & var2 can be anything (i.e. string which will contain only alphanumeric characters.)
One more thing -
I want to skip this rule if the url is -
http://www.abc.com/SKIPME
Please help me to write this rule!
Regards,
Shahu!

You would be better off defining a URI schema that tells you when something WILL be rewritten. For example...
http://www.abc.com/site/var1
http://www.abc.com/site/var2
That way, you can ensure you only ever apply the rule if the psuedo "site/" directory is browsed and not affect any other URI. This is the rewrite rule based on the above schema.
RewriteEngine on
RewriteRule ^site/([^/\.]+)/?$ index.php?u=$1 [L,NC,QSA]
Any other address, other than "/site/.../" would be unaffected by this rule, which means you don't need to worry about setting some addresses to be avoided. This keeps things as simple as possible.
You don't have to use "site" - you can use whatever name fits your purpose.

Related

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]

.htaccess, virtual directories, and semi-complex URLs

I'm basically just trying to have a master syntax for predictable URLs. Simple URL is no problem
RewriteEngine on
# RewriteRule ^friendlyUrl/content/?$ /index.php?app=main&module=content
Which to my understanding looks for the url structure and allows 1 or 0 trailing "/"'s
But some parts of the website have a /urlPrefix/ to access, eg. mysite.com/membersArea/
and /membersArea/ will be apart of every query there. I'm having trouble accomodating for trailing ?s and &s in URLs like these.
RewriteRule ^secureUrl/\?(.*)$ /index.php?app=admin&$1
This is my attempt to handle everything from mysite.com/secureUrl/ to mysite.com/secureUrl/?var1=foo&var2=bar and after many server errors and a search, I find myself here.
This is the most complex line I have and between you and me, I couldn't tell you exactly what's happening other than it looks for /friendlyUrl/10DIGITKEY/(possible task)/?possiblevars=foo&var2=bar
RewriteRule ^friendlyUrl/([a-zA-Z0-9]{10})/?([a-z]*)/?\??(.*)$ /index.php?app=main&module=web&id=$1&$2&$3
Htaccess has always been my weakest subject, and as a webmaster I pay the price constantly, any help would be appreciated.
Need to input the same request to the PHP file (plus ANY query with or without ? or &) whether its just /friendlyUrl/ or /friendyUrl/?var=1, /friendlyUrl/&var=1, /friendlyUrl/var=1
You're looking to keep the query string of your request URI to remain as is, or to be included in the rewritten URL after the rewrite process is done.
For this purpose, you use the QSA flag in your RewriteRule directive. So, to rewrite /friendlyUrl/10DIGITKEY/(possible task)/?possiblevars=foo&var2=bar, you'd have:
RewriteRule ^friendlyUrl/([a-z\d]{10})/([^/]*)/?$ /index.php?app=main&module=web&id=$1&task=$2 [QSA]
Notice the QSA flag at the end. Also, keep in mind that I'm passing the second match (the possible task of your URL) as another variable (named task). This variable will be empty if nothing was found.
QSA|qsappend
When the replacement URI contains a query string, the default behavior
of RewriteRule is to discard the existing query string, and replace it
with the newly generated one. Using the [QSA] flag causes the
query strings to be combined.

HTAccess Redirect using main parameter, ignore all others

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.

Htaccess rewrite with optional parameters

I have little understanding of HTACCESS, even though I have spent a long time looking into it. I think the real reason is because regular expressions go above and beyond my capacity.
Currently, in HTACCESS, I have use this line:
RewriteRule ^website/([^/]+)(/([^/]+))?$ controller/url.php?method=$1&param=$3
This will allow users to have a nice url that looks like www.website.com/website/method/optional_parameter
How could I add a second and third optional parameter to the HTACCESS rewrite line?
The easiest way would be to allow the second group to contain the rest of the URL after your "method" group.
RewriteRule ^website\/([^/]+)\/(.*)$ controller/url.php?method=$1&params=$2
Then use PHP to split params at each /
Hope that helps.

Resources