I've an https domain with WordPress installed in the root directory.
In the subdirectory "/test/" i made a web application that works using GET; the path is something like that:
https://www.example.com/test/file.php?url=urlname&etc=etc
I need to transform it in:
https://www.example.com/test/urlname/?etc=etc
I also need a redirect from the first type url to the second one.
It's the first time that i have to edit an htaccess file, and after searching on the web i tried this code
RewriteRule ^/?test/([^/]+)/$ test/file.php?url=$1&%{QUERY_STRING} [L,QSA]
RewriteCond %{REQUEST_URI} ^/test/file\.php$
RewriteCond %{QUERY_STRING} ^url=(.*)$
RewriteRule ^/?test/file\.php$ /test/%1/?%{QUERY_STRING} [L,R=301]
but it obviously doesn't work.
Can someone help me?
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /test/file\.php\?url=([^\s&]+)(?:&(\S*))?\s [NC]
RewriteRule ^ /test/%1?%2 [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^test/([^/]+)/?$ test/file.php?url=$1 [L,QSA,NC]
Related
I would like to show certain content on my website. I choose the content based on a URL parameter, like page=about. This works well, and my URL looks like example.com/mysite/index.php?page=about. My issue is that I would like my URL to read example.com/mysite/about, where the last part is the name of the page parameter.
I am attempting to do this through htaccess. This is my exact code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]
I see no change on my page nor to the URL structure. The URL still reads ...index.php?page=about. Am I doing something wrong? I tried this a bunch of different ways and I still get the same result. I also tried simply doing this, with no change:
RewriteEngine On
RewriteRule ^([a-z]+)\/([0-9]+)\/?$ index.php?page=$1 [NC]
I didn't think it was working, so I tried a redirect to some other site and it worked. Therefore, the htaccess file is getting read and able to function.
Any assistance would be helpful!
Thank you,
BP
You may use these 2 rules in your site root .htaccess:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+(mysite)/index\.php\?page=([^\s&]+) [NC]
RewriteRule ^ /%1/%2? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/mysite/([^/]+)/?$ [NC]
RewriteRule ^ index.php?page=%1 [L,QSA]
I have the following code:
RewriteEngine On
# If you need to use the `RewriteBase` directive, it should be the
# absolute physical path to the directory that contains this htaccess file.
#
# RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^category/(.*)$ /searchPage.php?crs_category=$1 [L,NC,QSA]
RewriteCond %{REQUEST_URI} !^/searchPage.php
RewriteRule ^ index.php [QSA,L]
This works great when I type category/business, but I also want the url to be written if someone type the old url such as searchPage.php?crs_category=business to be redirected as category/business with a 301 code letting the SEO knows that this is the new address.
Note:
The reason why there is the
RewriteCond %{REQUEST_URI} !^/searchPage.php
condition is because I am using the Slim framework and where I want all request to be redirected to index with the exception of the searchPage for routing.
Thanks in advance.
Add this right under the commented out RewriteBase /
RewriteCond %{THE_REQUEST} /searchPage\.php\?crs_category=([^&\ ]+)
RewriteRule ^ /category/%1? [L,R=301]
I am building a site using Slim, which suggests the following .htaccess code for creating pretty URLs:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
This means that a URL like http://example.com/account/login will alias for http://example.com/index.php/account/login, which is then processed by my front controller. So far so good.
However, if someone explicitly navigates to
http://example.com/index.php/account/login,
I would like it to first redirect to:
http://example.com/account/login,
and then alias for
http://example.com/index.php/account/login.
That way, the user will see http://example.com/account/login in their navigation bar.
How can I handle both of these rules simultaneously in .htaccess, and most importantly, without hardcoding the host and domain (http://example.com) in .htaccess?
This should also work in a subdirectory, for example:
http://example.com/site1/index.php/account/login ->
http://example.com/site1/account/login
With the .htaccess file located in the site1 directory relative to document root, and without having to explicitly put site1 in the .htaccess.
You need a new redirect rule before this rule:
RewriteEngine On
RewriteCond $0#%{REQUEST_URI} ^([^#]*)#(.*)\1$
RewriteRule ^.*$ - [E=BASE:%2]
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^index\.php(?:/(.*))?$ %{ENV:BASE}$1 [L,R=302,NC,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
Try this:
RewriteRule ^index\.php/(.*)$ /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
I've got a url http://example.com/pages.php/page-name and I want to remove the .php extension on the pages.php part... Is that possible with an .htaccess file or should I just give up?
I've tried RewriteRule ^(.+)\.php/(.+)$ $1/$2 from "Remove php extension from url" with no luck.
I'm new to .htaccess files (this is the first site my normal tricks don't work)
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+([^/.]+)\.php/(\S+)\s [NC]
RewriteRule ^ %1/%2? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/.]+)/([^/]+)/?$ $1.php/$2 [L,NC]
I have a setup that sets variables for the index page, but it also does the same for directories and I don't want it to do that. Here's my .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(login|images|favicon\.ico|home|about|sitemap|contactus|termsandconditions|privacypolicy|signup|search|careers|error|css|js) [NC]
RewriteRule ^([a-zA-Z0-9]+)$ index.php?name=$1
RewriteRule ^([a-zA-Z]+)/$ index.php?name=$1
RewriteRule ^([a-zA-Z]+)/([a-zA-Z0-9_]+)$ index.php?name=$1&page=$2
RewriteRule ^([a-zA-Z]+)/([a-zA-Z0-9_]+)/$ index.php?name=$1&page=$2
RewriteRule ^php/$ error/
Now, with this setup, if I type in mysite.com/login it will redirect to the index.php page and set login as the name variable. How do I make it to where it ignores the directories? This is frustrating me and I've looked through this site for over an hour for an answer and can't find a similar question (I might suck at that too, though. haha!).
Also, if you look at the last RewriteRule, you can see that I'm trying to redirect any attempt to access my php/ folder to my error/ folder. This is also not working.
RewriteCond only applies to the immediately following RewriteRule. Also, you can combine lines 3&4, and 5&6 respectively, by using /? on the end, which makes the / optional (regex).
Your file could be similar to this:
RewriteEngine On
#the following line will not rewrite to anything because of "-", & stop rewiting with [L]
RewriteRule ^(login|images|favicon\.ico|home|about|sitemap|contactus|termsandconditions|privacypolicy|signup|search|careers|error|css|js)/?(.*)$ - [L,NC]
RewriteRule ^php/?(.*)$ error/ [L,NC]
RewriteRule ^([^/]+)/?$ index.php?name=$1 [L]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?name=$1&page=$2 [L]
You may be interested in the Apache Mod_Rewrite Documentation.
RewriteCond %{REQUEST_URI} !^/(login|images|favicon\.ico|home|about|sitemap|contactus|termsandconditions|privacypolicy|signup|search|careers|error|css|js) [NC] !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
to either not execute the rule on a directory or a file
More info on the mod_rewrite documentation pages, search for CondPattern