.htaccess URL rewrite issue - .htaccess

I've run into a problem when trying to rewrite a url that has a # in it. The rewrite itself is simple:
RewriteRule ^accessories/access/Roll-Up-PR30/accessories.php#a485$ /alog/roll-up-product.php
But it just falls over and will not let me have the hash in there.
Any feedback is welcome as I am running out of ideas :S

Unfortunately most browsers (that I know of) don't send the fragment part of the URL to the server so it's not available in .htaccess; it's just a client-side anchor lookup.

It seems that hashes are a special case. See http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
The relevant section is "Extended Redirection"
RewriteRule ^xredirect:(.+) /path/to/nph-xredirect.cgi/$1 \
[T=application/x-httpd-cgi,L]

Related

Rewrite rule for URL with multiple query parameters in .htaccess

I'd like to be able to dynamically change the following URL type
www.website.com/listings/?q=&rtcl_location=city-state&rtcl_category=category-type
to look like
www.website.com/city-state/category-type/
I tried using [generateit.net/mod-rewrite/](Generate It's Mod Rewrite) but the only thing it changed was removing the q=%, the new URL looks like this:
www.webiste.com/?rtcl_location=city-state&rtcl_category=category-type
This is all latin to me and I appreciate any guidance.
Thanks
I tried reading multiple answers online and translate those answers to my own htaccess but have failed thus far.
I've also tried utilizing some generators online, as mentioned above, which have not given the intended result.
Sounds pretty straight forward, actually:
RewriteEngine on
# Redirect /listings/?q=&rtcl_location=city-state&rtcl_category=category-type
RewriteCond %{QUERY_STRING} ^(?:^|&)rtcl_location=([^&]+)&rtcl_category=([^&]+)(?:&|$)
RewriteRule ^/?listings/?$ /%1/%2/ [QSD,L,R=301]
# Rewrite /city-state/category-type/
RewriteRule ^/?([^/]+)/([^/]+)/?$ /listings/?rtcl_location=$1&rtcl_category=$2 [L]
That covers both, redirecting requests to the "old" URL and internally rewriting requests to the "new" URL.
I dropped the "q" query arg, it does not have any meaning according to what you wrote.
It is a good idea to start with a R=302 temporary redirection. And to only change that to a R=301 permanent redirection once everything works as desired. That precents nasy caching issues while testing. Nevertheless you should always test using a fresh anonymous browser window or clear or disable your browsers cache.

Need to rewrite URL contain # tag in htaccess

I need to rewrite URL in .htaccess? Here below is a description of my problem.
URL: www.example.com/about.php#vision
Needs to be rewritten as: www.example.com/vison.html
I tried this as mentioned in below, but it does not work
RewriteRule ^vison.html$ about.php#vision [L]
RewriteRule ^our-vision.html(.*)$ about.php$1 [L,NC]
The short answer is: it is not at all possible using .htaccess!
A longer answer:
The part after the # character is not sent from the browser to the web server, as it is only used client-side.
That type of "rewriting" must take place using client-side JavaScript.
The hashchange event might be a starting-point for you:
window.addEventListener('hashchange', function() {
var hash = location.hash.substr(1);
window.location.href = "/" + hash + ".html";
});
You should also look up SPAs (single page applications). There are lots of frameworks that will help you build solutions like these. Also, there are much more modern techniques that don't rely on hashes, like the popstate event and the mechanisms surrounding it.
A couple of frameworks that could help you get started:
AngularJS
Ionic
Update
When looking at your current .htaccess setup, it looks as if you want to do the reverse of what your question text implies - when someone requests the vison.html, you want to redirect them to about.php#vision - and not the other way around.
If that is true, then yes, you can do it using .htaccess by adding the R flag (REDIRECT) and also the NE flag (NO ESCAPE) to prevent the # character from being encoded:
RewriteRule ^vison.html$ about.php#vision [NE,R,L]

mod_rewrite: How to disable not clean urls navigation of rewrite rules

I've been enabled mod_rewrite module and all is right.
I created simple rules for the url, but how do I disable the url navigation (rewritten) with the parameters?
example:
# rewrite rule for cleaning
RewriteRule ^bookstore/([0-9]+)?$ /bookstore/book.php?id=$1 [L]
Now, if I navigate to http://mydomine.com/bookstore/123 all is done, but the url http://mydomine.com/bookstore/book.php?id=123 is also navigable.
How can I make visible and bavigable only the first one?
Add this to the same htaccess file:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /bookstore/book\.php\?id=([0-9]*)
RewriteRule ^bookstore/book\.php$ /bookstore/%1? [L,R=301]
This will 301 redirect requests for the URI with query strings to the one without.
Not 100% sure about this, but I think that if you rewrite A to B, then both A and B will work.
I would like to ask why exactly is it a problem that http://mydomine.com/bookstore/book.php?id=123 is navigable too? What is the problem if that link is valid too, and the user can use both links... although it would take them some time and luck to discover the second option. What would they gain by doing that? What would you lose? If the answer in both cases is "nothing", then simply stop worrying. :) If you used the old links previously and now replace then with new links, then it is a good thing that your customer's old bookmarks will still work.
But assuming that you have a good reason for disabling the old URLs, how about changing them both. For example rename "book.php" to "xyz.php" and then redirect http://mydomine.com/bookstore/123 to http://mydomine.com/bookstore/xyz.php?id=123 -- and the old http://mydomine.com/bookstore/book.php?id=123 will stop working.
Ok, that is an ugly solution, but you can make it nicer if instead of renaming the files you just move them to a subdirectory, like http://mydomine.com/xyz/bookstore/book.php?id=123 . Alternatively, you could use the redirect to add a "secret" parameter and then check it in the PHP file, for example rewrite http://mydomine.com/bookstore/123 to http://mydomine.com/bookstore/book.php?id=123&secret=xyz . Sure, it's just a "security by obscurity", but again... what exactly would anyone gain by discovering your true URLs?

.htaccess url masking

I have a couple of wordpress urls that I want to simplify for SEO purposes. I have
http://www.example.com/fr/product-fr/review-fr/
I would like to change that to http://www.example.com/fr/product/review/ (remove -fr) but without changing the URL internally. So the user would access http://www.example.com/fr/product/review/ but the server would serve the content of http://www.example.com/fr/product-fr/review-fr/.
I want to do this to get around a URL problem with Wordpress and WPML.
Appreciate the help
Assuming the two letter lang code is always going to be constant throughout the URL:
RewriteEngine on
RewriteRule ^([A-Za-z]{2})/product/review$ /$1/product-$1/review-$1 [L,QSA]
should work a treat :)
P.S. the regexp takes any two-letter code (upper or lower case) so will work with other langs should you require.
Have you tried mod_rewrite and Rewrite rules?
Try:
^/([a-zA-Z]{2})/([^\/]+)/([^\/]+)/$ /$1/$2-$1/$3-$1
Haven't tested this though..

url rewriting an id with a string variable

trying to figure out how to rewrite this url clientside
blog.com/post/how-to-get-the-ladies
to point serverside to
blog.com/post.php?id=123
i know how to do this:
blog.com/post/123
RewriteRule ^([^/]+)/?$ post.php?id=$1 [NC,L]
but how do you replace the id string with the post title slug?
The webserver itself doesn't make this distinction and cannot translate from your "unique text identifier" to the database id. Therefore a .htaccess rule alone evaluated by the webserver will not help you. But how is it done on all those web-applications? Normally this translation is done by Joomla/Wordpress itself and it only works as long the "how_to_get_the_ladies" text is known and unique throughout the system/database.
you can add rule that go to index file like :
RewriteRule ^(.*)$ index.php?url=$1
and in this file according to the title you can show the post that request
I solved a similar problem recently. I would suggest looking into the RewriteMap directive and using an 'External Rewriting Program'.
There are some big limitations with RewriteRule in terms of maintainability and robustness. If you haven't gotten there yet you may eventually. Only simple rewriting rules can be written safely.
With a rewriteMap you can create a php or perl script, take advantage of your existing code base, and perform all the rewriting rules from a localized place in your code which easily sits in version control.
I believe you need access to the httpd.conf (or vhost) configuration file though, RewriteMaps (or some related directive) cannot be put in .htaccess files.

Resources