I am getting some bugs hosting an application. The application uses rewrite rules. I did all tests possible and ensured that mod_rewrite is enabled in apache and the .htaccess file is being considered. I only want to visualize the URLs that is being genereted after considering the rewrite rule to continue the debug.
So my question is, given a .htaccess file with mod_rewrite turn on and rewrite rules defined in it, is it possible to view the final URL that is generated after considering the the rewrite rules ?
You can append %f to your LogFormat. This will show you the final filesystem path, which is more concise than looking at mod_rewrite tracing.
Related
Because of removing several languages from a multi-language website, I need to 301 redirect pages that end with ?lang=da, ?lang=de, and ?lang=nl to the same URLs but ending in ?lang=en. It sounds like a common scenario, but I haven't found the right code yet to accomplish this or tried some code because the purpose of that code was either to redirect to one new URL or to replace the URL but not the query string, while I need to replace the query string and keep the URL.
This probably is what you are looking for:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^lang=(da|de|nl)$
RewriteRule ^ %{REQUEST_URI}?lang=en [QSD,R=301,END]
For this to work the rewriting module obviously needs to be loaded into your http server and it has to be activated for your http host too.
It is a good idea to start out with a 302 redirection and to only change that into a 301 once you are convinced everything is set up as required. That way you prevent ugly caching effects...
You can implement such rules in the http server's host configuration. Or, if you do not have access to that, you can use a distributed configuration file (".htaccess"), but that has performance disadvantages and you also need to enable the interpretation of such files first. Please see the documentation of the tool you are using to learn how to do that.
My website currently online is completely static and all the URLs have a trailing slash at the end : https://www.website.com/blog/article-1/
I'm working on my new website which is using Prestashop. On Prestashop, URLs don't have a trailing slash : https://www.website.com/blog/article-1
Problem: I have an excellent SEO on my current website and I need to keep the actual URLs (with trailing slash) available. For user experience, I'd like URLs to work with or without trailing slash.
How can I redirect my new URLs to the same URL + trailing slash? If possible, I'd like to rewrite URLs so that users always see the URL with a trailing slash.
Example :
https://www.website.com/blog/article-1/ is redirected to https://www.website.com/blog/article-1 and the URL visible in the address bar is https://www.website.com/blog/article-1/.
Well, ask "How can I redirect my new URLs to the same URL + trailing slash"...
The answer obviously is: by implementing exactly that rule. There are thousands of examples for this alone here on SO. None of those helped? Why not?
Anyway, here is another one:
RewriteEngine on
RewriteRule ^/blog/([^/]+)$ /blog/$1/ [R=301]
RewriteRule ^/blog/([^/]+)/$ /blog/$1 [END]
You need to take care to send out references with leading slashes with this setup. Since otherwise your site will be dead slow, since the clients will have to request every single page twice due to the redirection then required for every single page...
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This rule will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).
If you mean default prestashop links like products, categories etc. you can just change their way to be built. Prestsahop allows us to achieve this within admin-panel Configure->Shop Parameters->Traffic & SEO->SEO and URL's>Schema of URLs (for PS 1.7).
And there change an URL in interest, for example, Route to category
from {id}-{rewrite} to {id}-{rewrite}/. And you won't need to redirect anything.
after a hacking attack i´ve got a few thousand spam url with "sohappy.php?xxxxxxx" in the search console and want to redirect them per .htaccess to the homepage of my website. Can anybody tell me the right syntax. Should this be done per redirct or rewrite?
Here is an sample url:
https://www.my-website.de/sohappy.php?1je2tvq.html
Cheers Alex
Most likely you simply want to rewrite all requests to the base path /sohappy. For that you want to implement a single redirection rule:
RewriteEngine on
RewriteRule ^/?sohappy\.php$ / [R=301]
And a general hint: you should always prefer to place such rules inside the http servers (virtual) host configuration instead of using dynamic configuration files (.htaccess style files). Those files are notoriously error prone, hard to debug and they really slow down the server. They are only provided as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).
I want to redirect some pages of my site.
These pages have the following form:
xxxx.html (x=digit, up to 4 digits), eg 1.html, 24.html, 6875.html etc
I want to redirect these pages to the following format:
index.php?id=xxxx.
The solution I have already apply is the following line in the htaccess file:
RewriteRule ([0-9]+)\.html$ /index.php?id=$1 [R=302,QSA,L]
The redirection works perfectly but the problem is that I want to limit this rule for pages that have up to 4 digits, eg 6789.html, 234.html.
For example I don't want the rule to be applied for the page 55555.html.
This is probably what you are looking for:
RewriteEngine on
RewriteRule ^/?([0-9]{1,4})\.html$ /index.php?id=$1 [R=302,QSA,L]
That slightly modified pattern will work in the http servers host configuration and likewise in dynamic configuration files (.htaccess style files).
And a general hint: you should always prefer to place such rules inside the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those files are notoriously error prone, hard to debug and they really slow down the server. They are only provided as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).
How can I hide the name of a file in the url bar?
something like:
www.test.com/something/
and not
www.test.com/something/index.php
You need to use rewrite rules. If you use Apache web server than mod_rewrite is a good choice.
This manual should walk you thought the process of creating successfull rewrite rules : http://www.workingwith.me.uk/articles/scripting/mod_rewrite