Removing duplicate slashes in URL via .htaccess or PHP - php

I'm trying to remove duplicate slashes from URLs. The following .htaccess rule:
RewriteRule ^(.+)//+(.*)$ $1/$2 [L,NC,R=301]
do NOT work for me on a URL such as the following:
http://www.mp7.org/?site=69.com\\\\\\\\\\\\\\\\
The .htaccess file
#### mod_rewrite in use
Options +FollowSymlinks
RewriteEngine On

This rule won't work for backslashes. You must add a similar rule with backslashes
RewriteRule ^(.+)\\\\+(.*)$ $1\\$2 [L,R]
If you want to replace backslashes with (forward) slashes, use this rule instead
RewriteRule ^(.+)\\\\+(.*)$ $1/$2 [L,R]
And to remove all back-/slashes at the end of the request
RewriteRule ^(.*?)[/\\]+$ $1 [L,R]
and the same when it is a query string
RewriteCond %{QUERY_STRING} ^(.*=.*?)[/\\]+$
RewriteRule ^.*$ $0?%1 [R,L,QSA]
When everything works as you expect, you can change R to R=301.
Never test with 301 enabled, see this answer
Tips for debugging .htaccess rewrite rules
for details.

This is because of IIS server, and mainly almost certainly your site is urbanized in .Net .asp or . aspx You need to put into service one miniature script, when your server be given http or https demand it must Lcase(URL). If there are not a lot of pages, you can rename all within lowercase, modify all inner linking of WebPages to lowercase and after that appeal URL removal request in Google webmaster tools.
how to remove duplicate urls

Related

Htaccess rewriting rule for localhost

The project is already running in prod, the developer who added that rule is no longer working with us but it is pretty working in prod, so in order to have the project working locally I tried many options, now I set the SSL in my web server and set virtual host to match exactly the prod so that it can run correctly, in the code the sign form is sent by session but it is blocked for security reason and I am pretty sure that it is related to htaccess and here are the full rules mentioned in htaccess: # -- SITE PAGES ---
RewriteRule ^(.+)\.html$ index.php?_htaccess_url=$1&%{QUERY_STRING} [L]
RewriteRule https://www.sanatariol.com(.+)\.html$ index.php?_htaccess_url=$1&%{QUERY_STRING} [L]
The URL of sign page is: https://www.sanitavia.com/cabchatt/connexion.html
Do I have to add a rule in htacces since I want it to run locally?
the rule is correct but try to add this just before your rule
RewriteBase /
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule https://www.sanatariol.com(.+)\.html$ index.php?_htaccess_url=$1&%{QUERY_STRING} [L]
The RewriteRule directive matches against the URL-path only (not the absolute URL). So, the above rule will not match.
Try the following instead:
RewriteRule (.+)\.html$ index.php?_htaccess_url=$1 [QSA,L]
You also don't need to manually append the query string, use the QSA (Query String Append) flag instead.

.htaccess redirect and rewrite url if REQUEST_URI starts with specific path

I am working on a Wordpress site, it requires a plugin that needs to access the API endpoint of WP.
It wants to access the site's:
http://myhost.test/wp/wp-json/erp/v1/hrm/employees/1\?include\=department,designation,reporting_to,avatar,roles
but changes to the folder structure, causes the real, and actual link should be:
http://myhost.test/wp-json/erp/v1/hrm/employees/1\?include\=department,designation,reporting_to,avatar,roles
as you can see, there should be no /wp at the start of the %{REQUEST_URI}
I am trying to make this work: but it won't redirect the request:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/wp/wp\-json/
RewriteRule ^/wp/wp\-json(.*) /wp\-json$1 [L,R]
I do not understand what I am doing wrong,
I am catching everything after ^/wp/wp-json then forward it to ^/wp-json
What is going on?
Regards,
Using mod_rewrite in .htaccess context vs a <Directory> or <VirtualHost> context each have slightly different requirements in how the patterns are parsed. Most importantly to your situation, in .htaccess the first argument to RewriteRule is not matched against a leading slash / because the pattern is considered relative to the directory it is in.
Remove the leading / from your RewriteRule matcher argument:
RewriteRule ^wp/wp\-json(.*) /wp\-json$1 [L,R]

URL Rewriting .php to .html

I'm converting my urls extension from .php to .html in my .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} (.).php
RewriteRule ^(.*).php $1.html [R=301,L]
RewriteRule ^(.*).html $1.php [L]
FallbackResource /index.php
The problem is that I have some section with the word "php" on it:
www.mywebsite.com/phpscripts.php
And when it is converted:
www.mywebsite.com/htmlscripts.html
^(.*).php
This regex says: anything, including nothing, followed by a single arbitrary character, followed by "php". For example, it'll match "blahphp.html", specifically it'll match the "blahphp" part and not care about the extension at all.
What you're looking for is this:
RewriteRule (.+)\.php$ $1.html [R=301,L]
RewriteRule (.+)\.html$ $1.php [L]
(.+)\.php$ is something (at least one character) followed by a period followed by "php" at the end of the string. You can also get rid of the RewriteCond, it doesn't add anything to these rules.
Also note that you should be changing all your HTML files to link to href="...html". Don't rely on these redirects alone to fix your problem; not only is it inefficient to redirect every single request, it'll also break POST requests. It's only acceptable to redirect clients which try to open the old URLs for whatever reason to the new canonical URLs.

URL Rewrite rule in .htaccess for Root directory

I am using this rule to rewrite the link
RewriteEngine On
RewriteRule ^(.*)/(.*) show_cv.php?email=$1
It is working fine like if I write this url with last slash
www.mysite.com/letschat_2008#yahoo.com/ ----> index.php?email=letschat_2008#yahoo.com
But when I remove the last slash from the link www.mysite.com/letschat_2008#yahoo.com/ it shows error 404.
I wish the URL Rewrite rule would work for both with slash and without slash (/)
www.mysite.com/letschat_2008#yahoo.com/ ----> index.php?email=letschat_2008#yahoo.com
www.mysite.com/letschat_2008#yahoo.com ----> index.php?email=letschat_2008#yahoo.com
Your rules are looping, you need to make sure you are rewriting an email address, and add some conditions so that the rule doesn't get applied if it's accessing an existing resource:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9_\-\#\.]+)/?$ /show_cv.php?email=$1 [L]
You should then be using the following rule:
RewriteRule ^(.*)/? show_cv.php?email=$1
I assume you note these rules in a .htaccess file, not in the server configuration when looking at your description ?
Rethink if you don-t want to put this into the server configuration. Apart from the usage of .htaccess files being harder to debug using rewrite rules in those files is more complex than in the server configuration. This is documented in mod_rewrites docs.
The reason for the behaviour is the different content of REQUEST_URI in both cases. Have a try checking this directly and you will see the problem. The whole part "letschat_2008#yahoo.com" is simply missing in that variable in case 2 (no "/"). To get this working you must use an additional rewriteCondition (also documented...). Something like these untested lines:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/(.+)&
RewriteRule - show_cv.php?email=%1
(note the '%' instead of a '$' in the last line)

URL rewriting-code for rewriting

i am working for a site,which is in php.....i want to rewrite url
e.g www.3idiots.co.in/stories.php?id=17
if i want to rewrite it as
www.3idiots.co.in/stories/17.html
can any one tell me the code for this to write in .htaccess file.?
I'm assuming you're using Apache with mod_rewrite. Something like
RewriteEngine On
RewriteRule ^/stories/([0-9]+)\.html /stories.php?id=$1
should do the trick. Of course you'll need to make sure that RewriteRule is allowed in that directory. See this wiki page for more information.
mod_rewrite can only rewrite/redirect requested URIs and not those that are in your HTML documents. So you should first make sure, that your PHP application is printing the correct URIs, so /stories/17.html instead of /stories.php?id=17.
After that, you can use the rule suggested by José Basilio:
RewriteRule ^stories/([0-9]+)\.html$ stories.php?id=$1
Though redirecting requests of /stories.php?id=17 externally to /stories/17.html and then internally back to /stories.php?id=17 is possible, it’s not good practice as that would result in twice as many requests. But here’s the rule for that:
RewriteCond %{THE_REQUEST} ^GET\ /stories\.php[?\s]
RewriteCond %{QUERY_STRING} ^(([^&]*&)*)id=([0-9]+)&*([^&].*)?$
RewriteRule ^stories\.php$ /stories/%3.html?%1%4 [L,R=301]

Categories