Htaccess adding slash on end of url - .htaccess

I have to made some changes in my htaccess if it is possible,
There is my htaccess:
RewriteRule ^blog/([a-zA-Z0-9.\/\-\?\&]+)/.php$ blog.php?id=$1
It is rewrite for get method wich one hide ?id= and it's work perfectly, so i have problem if i take of from url slash (/) then i get 404 erorr.
I need to made RewriteRull which one make slash on end of url if is url without slash.
So if is possible to make some if statement which one detect url without slash and add it. Or make RewriteRull for that url?

Adding this to your .htaccess file may just what you want:
DirectorySlash On

Related

Rewriting a specific URL: adding a subdirectory

I am working on a WordPress-site and I have an URL like
https://example.com/technische-uebersetzungen
What I need is:
https://example.com/subdirectory/technische-uebersetzungen
My code has a bug, it does not rewrite the URL. I placed it at the end of the existing .htaccess file.
RewriteEngine On
RewriteRule ^technische-uebersetzungen/(.*)$ /blog/technische-uebersetzungen/$1 [R=301,NC,L]
Does anybody know, what is that bug?
My code has a bug, it does not rewrite the URL. I placed it at the end of the existing .htaccess file.
One "bug" is placing that rule at the end of the .htaccess file. By placing the rule after the WordPress code block, the request is first rewritten to index.php and your rule is never processed.
This "redirect" needs to go at the top of the .htaccess file, before the WordPress code block (ie. before the # BEGIN WordPress comment marker).
RewriteRule ^technische-uebersetzungen/(.*)$ /blog/technische-uebersetzungen/$1 [R=301,NC,L]
This rule matches /technische-uebersetzungen/<anything> (note the mandatory trailing slash after the first path segment) and the <anything> part is copied onto the end of the target URL. This is not mentioned in your example.
Your example URL is for /technische-uebersetzungen exactly (no trailing slash). And nothing is appended to the end of the target URL.
Try the following instead at the top of the .htaccess file:
RewriteRule ^technische-uebersetzungen(/.*)?$ /blog/$0 [R=301,NC,L]
This will match /technische-uebersetzungen or /technische-uebersetzungen/<anything> and redirect to the same URL with a /blog/ prefix.
NB: You must test this first with a 302 (temporary) redirect to avoid any potential caching issues and only change this to a 301 (permanent) redirect - if that is the intention - once you have confirmed this works as intended.

HTACCESS: Rewrite a Previously Rewritten URL Causes Error

not sure what my problem is, but I insert one rewrite into my htaccess file and the whole thing breaks. I used an htaccess tester and it says the rule works fine.
For some reason Google indexed the php file that I normally rewrite URLs to. So I need to take the .php page that displays and send it to the proper rewritten URL.
Here is the rule:
RewriteRule ^products/Automotive-Transmission-Torque-Converters-results.php$ /Search-Results [QSA,R=301,L]
The only thing I can think that would be causing the problem, is that I use this URL in another rewrite later on in my code.
Here is the second rewrite later on in the file:
RewriteRule ^Search-Results/?$ /products/Automotive-Transmission-Torque-Converters-results.php [QSA]
I hope I've identified the problem correctly. But again, I put the first rewrite rule into my htaccess and the the regular page doesn't show, the browser says there is an error on the page. Yet, htaccess testers say the rule works fine. Hence my suspicion.
RewriteRule ^products/Automotive-Transmission-Torque-Converters-results.php$ /Search-Results [QSA,R=301,L]
RewriteRule ^Search-Results/?$ /products/Automotive-Transmission-Torque-Converters-results.php [QSA]
Yes, these two directives used together will result in a redirect-loop (something that online testers don't appear to check for). The problem is that the first rule will trigger an external redirect after the second rule has rewritten the URL (when the rewrite engine starts over - in .htaccess).
The solution is to make sure the first rule (the external redirect) only triggers on direct requests from the client and not requests that have been internally rewritten.
We can check for direct HTTP requests by checking against the REDIRECT_STATUS environment variable, which is empty on the initial request and set to "200" (as in 200 OK HTTP status) after the first successful rewrite.
For example:
# Redirect direct request for PHP file to canonical URL
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^products/Automotive-Transmission-Torque-Converters-results\.php$ /Search-Results [R=301,L]
# Internally rewrite the canonical URL
RewriteRule ^Search-Results/?$ /products/Automotive-Transmission-Torque-Converters-results.php [L]
Note that I have removed the QSA from both rules, since it's not required here (the query string is appended by default). And I've included the L flag on the second rule. Also, don't forget to backslash escape the literal dot in the RewriteRule pattern.
Test first with 302 (temporary) redirect to avoid potential caching issues and make sure your browser cached is cleared before testing.
For some reason Google indexed the php file that I normally rewrite URLs to.
However, you need to try and identify how Google managed to find the PHP file/URL in the first place. If this is the a result of an incorrect internal link on your site then it needs to be fixed.

rewriterule how to handle no ending slash

I try to redirect anything coming into a directory to another. the rewriterule command I use is :
RewriteRule ^VirtualDirectory(.*)$ GENS/RealDirectory$1 [L,NC]
The thing is that I'd like that a query with no end slash to VirtualDirectory to be rewritten as if the query was for VirtualDirectory/
The behaviour I get is :
query to VirtualDirectory/ works great without the user noticing
query to VirtualDirectory workd great but the url shown in the brwoser is : GENS/RealDirectory/
I've tried many things but I can't get the behaviour I want.
If I add a rule
RewriteRule ^VirtualDirectory$ GENS/RealDirectory/ [L,NC]
to handle that specific case, it works great except that all resources of the page are rewrited to the folder before VirtualDirectory
What's happening is the internal rewrite is happening without a trailing slash, then mod_dir takes over and does a browser redirect to the same URL but with a trailing slash. You can turn off mod_dir by using DirectorySlash Off in your .htaccess file. If you want the trailing slash always, try changing the rule to this:
RewriteRule ^VirtualDirectory/?(.*)$ GENS/RealDirectory/$1 [L,NC]

htaccess QUERY_STRING urldecode

I have a problem concerning .htaccess and QUERY_STRING.
I try redirecting an URL with my htaccess that looks like this:
http://mydomain.tld/out/http%3A%2F%2Fotherdomain.tld%3Fparam%3D0
to
http://otherdomain.tld?param=0
I use RewriteCond and RewriteRule with the REQUEST_URI to redirect the url and everything works fine since REQUEST_URI is urldecoded by default in the htaccess.
However, when I email the link to Hotmail, Hotmail urldecodes the slashes and the question mark. The result looks like this:
http://mydomain.tld/out/http%3A//Fotherdomain.tld?param%3D0
So htaccess takes the link and tries to redirect it but due to the question mark the htaccess "thinks" everything behind the question mark is a QUERY_STRING.
The problem: apache2 doesn't urldecode the QUERY_STRING. So what happens is that htaccess redirects to
http://otherdomain.tld?param%3D0
which will fail.
So my question is:
How can I tell htaccess to either urldecode the QUERY_STRING or use the full requested url (either urlendcoded or urldecoded) including the part after the question mark
Thanks in advance!
Cheers
You need not add [QSA] to your rewrite rule to force htaccess to encode query string too.
http://httpd.apache.org/docs/current/en/mod/mod_rewrite.html

Remove slugs and redirect using htaccess

Using .htaccess, how can I strip parameters from a url and redirect an entire folder to another location?
For example, I have http://www.webbiscuit.co.uk/News.aspx/11/favicon-competition and I just want to redirect this to http://www.webbiscuit.co.uk/.
I have tried
Redirect 301 /News.aspx http://www.webbiscuit.co.uk/
but that just strips off the News.aspx part, and redirects me to
http://www.webbiscuit.co.uk/11/favicon-competition
How can I strip off the entire file and the slugs?
Using mod_rewrite (the ? may be unnecessary here):
RewriteRule ^News\.aspx(.*) http://www.webbiscuit.co.uk/? [NC,R=301,L]

Resources