Rewrite rule doesn't change the browser url - apache

I have the following problem:
current url:
mysite.com/Category.php?Category=Category name
Want it to show up in the browser as follows:
mysite.com/Category name
I have the following in my .htaccess file
RewriteRule ^([a-zA-Z0-9_-]+)$ Category.php?Category=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ Category.php?Category=$1
but this doesn't seem to do anything
Thanks for looking
Current rules:
rewriteengine on
rewritecond %{HTTP_HOST} ^www.mysite.co.uk$ [OR]
rewritecond %{HTTP_HOST} ^musite1.co.uk$
rewriterule ^(.*)$ "http\:\/\/mysite\.co\.uk\/$1" [R=301,L] #5311a623b538f
rewritecond %{HTTP_HOST} ^www.mysite.co.uk$ [OR]
rewritecond %{HTTP_HOST} ^mysite.co.uk$
rewriterule ^Pianoshop\/detailed_page\.php(.*)$ "http\:\/\/mysite\.co\.uk\/product\.php$1" [R=301,L] #5310700a7e477
rewritecond %{HTTP_HOST} ^www.mysite.co.uk$ [OR]
rewritecond %{HTTP_HOST} ^mysite.co.uk$
rewriterule ^Pianoshop\/index_blog\.php(.*)$ "http\:\/\/mysite\.co\.uk\/news\.php$1" [R=301,L] #53

You need these 2 rules:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+Category\.php\?Category=([^\s&]+) [NC]
RewriteRule ^ %1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ Category.php?Category=$1 [L,QSA]

Do you have mod rewrite enabled ?
Your .htaccess should look like this :
RewriteEngine on
RewriteRule ^([a-zA-Z0-9_-]+)/?$ Category.php?Category=$1

Related

How to write clean url in reverse?

I have the following in my my htaccess
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^terms-and-conditions terms.php
RewriteRule ^privacy privacy.php
RewriteRule ^thank-you thanks.php
How could I also do this in reverse such as typing /privacy.php is rewritten as /privacy.
You need to use 3 separate rules using %{THE_REQUEST} variable to avoid redirect loop:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,NE,L]
RewriteCond %{THE_REQUEST} \s/+terms\.php[\s?] [NC]
RewriteRule ^ /terms-and-conditions [R=301,L]
RewriteCond %{THE_REQUEST} \s/+privacy\.php[\s?] [NC]
RewriteRule ^ /privacy [R=301,L]
RewriteCond %{THE_REQUEST} \s/+thanks\.php[\s?] [NC]
RewriteRule ^ /thank-you [R=301,L]
RewriteRule ^terms-and-conditions/?$ terms.php [L,NC]
RewriteRule ^privacy/?$ privacy.php [L,NC]
RewriteRule ^thank-you/?$ thanks.php [L,NC]

Add HTTPS to already existing Mod Rewrite

I have the following rules and conditions in my mod_rewrite and like to add HTTPS to it. I like to confirm the following it accurate before publishing it live.
Current Rules and Conditions
RewriteEngine On
RewriteCond %{REQUEST_URI} \.jpg$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* /home/domain.com/public_html/missing.png [L]
RewriteCond %{HTTP_HOST} ^www.olddomain.com$
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteRule ^index.html$ http://www.domain.com/ [L,R=301]
New Rules and Conditions
RewriteEngine On
RewriteCond %{REQUEST_URI} \.jpg$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* /home/domain.com/public_html/missing.png [L]
RewriteCond %{HTTP_HOST} ^www.olddomain.com$
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]
RewriteCond %{HTTPS} Off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteRule ^index.html$ https://www.domain.com/ [L,R=301]
RewriteEngine On
# always do domain/https redirs first
RewriteCond %{HTTPS} =off [OR]
RewriteCond %{HTTP_HOST} ^(?!www\.domain\.com$). [NC]
RewriteCond %{THE_REQUEST} ^\S+\s+/?(\S*)
RewriteRule ^ https://www.domain.com/%1 [NS,NE,L,R=301]
# remove index files from URLs, except from POST forms, etc. you can add more file extensions to the second condition
RewriteCond %{REQUEST_METHOD} ^(?:GET|HEAD)$
RewriteCond %{THE_REQUEST} ^\S+\s+/?((?:[^?/]*/)*?)index\.(?:html?|php|pl)(\?\S*)?(?:\s|$)
RewriteRule ^ https://www.domain.com/%1%2 [NS,NE,L,R=301]
# missing images
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.(?:jpe?g|png|gif)$ /missing.png [NS,NC,L,DPI]

htaccess redirection issue in local server

When I am trying to run my project in local server (WAMP), it is redirecting to www.localhost.com
When I type localhost/MYPROJECTNAME then it is redirecting to www.localhost.com
Please find below code:
RewriteEngine on
RewriteBase /MYPROJECTNAME
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php?/$0 [PT,L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Try this, it shouldn't redirect if it's localhost.
RewriteEngine on
RewriteBase /MYPROJECTNAME
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php?/$0 [PT,L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^localhost [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Negate for localhost and more importantly change the order of your rules:
RewriteEngine on
RewriteBase /MYPROJECTNAME/
RewriteCond %{HTTP_HOST} !^(www\.|localhost) [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php?/$0 [PT,L]

.htaccess redirect for complex url

I have an website with the following htaccess file:
RewriteRule ^declinare/([^/]+)/? declinare.php?cuvant=$1 [L,NC,QSA]
RewriteRule ^conjugare/([^/]+)/? conjugare.php?cuvant=$1 [L,NC,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]*)/?$ definitie.php?forma=$1&cuvant=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]*)/([^/]*)/?$ definitie.php?forma=$1&cuvant=$2&pagina=$3 [L,QSA]
RewriteCond %{HTTP_HOST} ^toatecuvintele\.ro$ [OR]
RewriteCond %{HTTP_HOST} ^www\.toatecuvintele\.ro$
RewriteRule ^index\.php$ "http\:\/\/www\.toatecuvintele\.ro\/" [R=301,L]
RewriteCond %{HTTP_HOST} ^toatecuvintele.ro$
RewriteRule ^/?$ "http\:\/\/www\.toatecuvintele\.ro\/" [R=301,L]
RewriteCond %{HTTP_HOST} ^37\.156\.32\.57
RewriteRule (.*) http://www.toatecuvintele.ro/$1 [R=301,L]
Everything works fine except for one thing:
http://toatecuvintele.ro/cuvant/test does not redirect automatically to http://www.toatecuvintele.ro/cuvant/test
How can I do to redirect everything withc is Non WWW to www?
Thank you.
You can do that:
RewriteCond %{HTTP_HOST} ^toatecuvintele\.ro$
RewriteRule ^/?(.*) http://www.toatecuvintele.ro/$1" [R=301,L]
Have you tried that:
RewriteCond %{HTTP_HOST} ^toatecuvintele\.ro$ [OR]
RewriteCond %{HTTP_HOST} ^www\.toatecuvintele\.ro$
RewriteRule ^(.*)$ http://www.toatecuvintele.ro$1" [R=301,L]

getting htaccess rewrite rules to rewrite only specific urls

I only want to rewrite this specific url:
http://www.website.co.uk/php/project.php?project=anythingHere
to
http://www.website.co.uk/php/project/anythingHere
this works fine but when I go to http://www.website.co.uk/php/project/anythingHere
it has changed all the links as well (style sheets, javascript, hyperlinks... things I don't want to change). for example:
http://www.website.co.uk/php/index.php
to
http://www.website.co.uk/php/project/index.php
This is what I have already
RewriteEngine On
RewriteCond %{HTTP_HOST} ^website.com\.co\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.website.com\.co\.uk$
RewriteRule ^clients$ "http\:\/\/website.com\.co\.uk\/billing\/index\.php" [R=301]
RewriteRule ^project/([^/]*)$ /php/project.php?project=$1
Can you try this code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?website\.co\.uk$ [NC]
RewriteRule ^clients/?$ http://website.co.uk/billing/index.php [NC,L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(php/project)\.php\?project=([^\s&]+) [NC]
RewriteRule ^ /%1/%2? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(php/project)/([^/]+)/?$ /$1.php?project=$2 [L,QSA,NC]