My old website has a url like "/index.php?s=subsite" and "/?s=subsite".
Now I added the following lines to ".htaccess" file, to make them shorter:
RewriteEngine On
RewriteBase /
RewriteRule ^(.+)\.(html)?$ index.php?s=$1&%{QUERY_STRING} [L]
Open a site with "/subsite.html" works fine, but also the old url "/index.php?s=subsite" and "/?s=subsite" works too. Is there a way, to allow "/subsite.html" only and redirect the old requests to it?
Replace your code with this code:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(?:index\.php)?\?s=([^\s]+) [NC]
RewriteRule ^ /%1.html? [R=301,L]
RewriteRule ^([^.]+)\.html$ /index.php?s=$1 [L,NC,QSA]
Add this rule to your htaccess file:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?s=([^& \]+)&?([^\ ]*)
RewriteRule ^ /%1.html?%2 [L,R=301]
This matches against the request, tries to capture the s query string parameter, then redirects the browser to that parameter followed by an ".html" and the rest of any query string that was there.
Related
I want to create pretty url. But, I got some problem with .htaccess. For example I have url domain/some.php?f=query-string.
I want to change domain/query-string (expected url). Is that possible to change / redirect via .htaccess. Or maybe from php file itsself.
this is a bit of htaccess snippet i made, but i get it blank/error page
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteRule ^/([^/.]+)$ some.php?f=$1 [NC,L]
</IfModule>
Thanks for your attention.
RewriteRule ^/([^/.]+)$ some.php?f=$1 [NC,L]
In .htaccess, the URL-path matched by the RewriteRule pattern does not start with a slash, so the above will never match and it will do nothing. This should be written like the following instead:
RewriteRule ^([^/.]+)$ some.php?f=$1 [L]
The NC flag is not required here, since the regex is already "case-insensitive".
I have a url like this:
http://www.localhost.com/code_category/computers/
I want to change this url to:
http://www.localhost.com/category/computers/
I don't need url redirection.
My current htaccess file looks like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
You only want to redirect code_category to categoryexternally and keep the path as it is internally so, try this :
RewriteCond %{THE_REQUEST} !\s/+category/ [NC]
RewriteRule ^code_category/(.*)$ category/$1 [R=302,L,NE]
RewriteRule ^category/(.*)$ code_category/$1 [L]
The above will redirect any request containscode_category/whatever to category/whatever externally and keep the internal path as it is .
If you want only request contains code_category/computers/ change it to this :
RewriteCond %{THE_REQUEST} !\s/+category/computers/ [NC]
RewriteRule ^code_category/computers/(.*)$ category/computers/$1 [R=302,L,NE]
RewriteRule ^category/computers/(.*)$ code_category/computers/$1 [L]
test it , if it is fine change 302 to 301 for permanent redirection.
Note: clear your browser cache then test it.
.htaccess file
Add this code
RewriteEngine on
RewriteCond %{HTTP_HOST} ^localhost.com [NC,OR]
# without redirect
# RewriteRule ^/code_category/computers/$ category/computers/
RewriteRule ^/category/computers/$ code_category/computers/
# redirect method
# RedirectMatch 301 ^/code_category/computers/$ category/computers/
RewriteEngine On enables mod_rewrite.
RewriteCond %{HTTP_HOST} shows which URLs we do and don't want to run through the rewrite.
In this case, we want to match example.com.
! means "not." We don't want to rewrite a URL that already includes folder1, because then it would keep getting folder1 added, and it would become an infinitely long URL.
[NC] matches both upper- and lower-case versions of the URL.
RewriteRule defines a particular rule.
The first string of characters after RewriteRule defines what the original URL looks like. There's a more detailed explanation of the special characters at the end of this article.
The second string after RewriteRule defines the new URL. This is in relation to the document root (html) directory. / means the html directory itself, and subfolders can also be specified.
For Reference click here
Hope this helps!
I want to replace some parts of url using htaccess.
I am using below code but i am getting 404 error.
RewriteRule ^/v1/surveys/login(.*)$ /oauth2/rest/token/$1 [R=301,L]
This is my url http://192.168.1.10/survey/api/v1/surveys/login and i want to
replace with http://192.168.1.10/survey/api/oauth2/rest/token
You can use this rule just below RewriteEngine On line:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/(survey/api)/v1/surveys/login(/.*)?$ [NC]
RewriteRule ^ /%1/oauth2/rest/token%2 [R=301,L,NE]
# rest of the rules go below this
My entire .htaccess file consists of the following code:
Options +FollowSymLinks -MultiViews
rewriteEngine on
RewriteBase /
## Hide .php extension by external redirection:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,NC]
## Internally redirect to .php extension:
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php
## Redirect to index when page is missing.
ErrorDocument 404 http://www.domain.com
It (finally!) removes the *.php extension at the end. No complaints. But I've been struggling with inserting additional code to add a trailing slash (/). Nothing seems to work. Sometimes CSS is shut down and adding a slash after %1 results in errors.
In addition, I've read all kinds of stories online that using Multiviews and trailing slashes can create duplicate urls and other search engine problems. Maybe it's best to leave it as it is?
Can anybody give me some insight in the code to use here?
The trailing slash in the %1 is needed. The broken CSS is probably because you're using relative URL's in your content and the trailing slash changes the URI base. To fix that you need to either make all your links absolute URLs or add a base to your page header:
<base href="/" />
Then you'll need to change the internal rewrite that handles adding the php back, so something like:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
## Hide .php extension by external redirection:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+([^.]+)\.php [NC]
RewriteRule ^ /%1/ [R,NC,L]
## Internally redirect to .php extension:
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]
## Redirect to index when page is missing.
ErrorDocument 404 http://www.domain.com
It seems like a lot of code to remove .php for me it's enough using
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
As for the Slash part it depends on your setup really.
but if you were to base your website from index.php
eg:
$key = $_GET["key"];
if($key = "about"){
include("about.php");
}
else if($key = "contact"){
include("contact.php");
}
else{
include("index.php");
}
and use the following code in .htacces
#RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?key=$1
#RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?key=$1
Works for me! :)
I need some htaccess rules which rewrite URIs like this:
http://sub.domain.tld/en/about?a=1&b=2
to this:
http://sub.domain.tld/about.php?lang=en&a=1&b=2
or more simple:
http://sub.domain.tld/about.php?a=1&b=2&lang=en
No difference...
But the user should see the first URI not the converted one (shouldn't be redirected).
You may try this:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/([^/]+)/([^/]+) [NC]
RewriteCond %{QUERY_STRING} ([^/]+) [NC]
RewriteRule .* %2.php?%3&lang=%1? [L]
Maps silently
http://sub.domain.tld/LangCode/FileName?key1=val1&key2=val2
With or without trailing slash. Always displayed in browser's address bar,
To:
http://sub.domain.tld/FileName.php?key1=val1&key2=val2&lang=LangCode
For permanent redirect, replace [L] with [R=301,L]
Here is the .htaccess code :
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule en/about?a=(.*)&b=(.*)$ about.php?lang=en&a=$1&b=$2