I have a URL that needs to be reformatted. example.com/?article_category=lifestyle I want this to be formatted to: example.com/lifestyle. However, when I try to reformat it with my .htaccess file it doesn't work. My .htaccess file is located in the same directory as my file. The file name is index.php.
Here's the rewrite rule I proposed:
RewriteRule ^([\w-]+)/?$ index.php?article_category=$1 [QSA,L]
I don't know why this isn't working. What am I doing wrong?
Useful Docs: RewriteCond Directive & RewriteRule Directive
RewriteCond %{QUERY_STRING} article_category=([a-z]+)
RewriteRule ^/?$ /%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z]+) index.php?article_category=$1 [QSA,L]
Line 1: checking if query string containing article_category=([a-z]+), article_category=lifestyle matched
Line 2: if the URL is example.com or example.com/, redirect permanently to /lifestyle, %1 is a RewriteCond backreference, grouped parts (in parentheses ([a-z]+)) of the pattern
Line 3 / 4: if the URL is not regular files and directories
Line 5: if the URL is starting with alphabetic, rewrite the URL to index.php?article_category=$1
Related
I havve been using an htaccess file to create rewrites so as not to not have to include .php/.html filenames in my urls.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example-page.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example-page.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule /front /front_page.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule /redirect /redirect-page.php
However as my code is currently written the redirects are not working and all attempts at loading the pages with anything else besides the filenames is giving me a 404 error. How might i fix my code to get the redirecting to work?
Write the directives like this instead:
RewriteEngine on
RewriteRule ^front$ /front_page.php [L]
RewriteRule ^redirect$ /redirect-page.php [L]
There doesn't seem to be a need for all your additional conditions? I assume you only have the one domain example-page.com and you don't have a file called /front etc.
In .htaccess, the URL-path matched by the RewriteRule pattern does not start with a slash. You should also include start/end-of-string anchors on the regex so that you only match front exactly and not match it anywhere in the URL-path.
I want to change URL in .htaccess but I don't know how to do anyone can help me please.
RewriteRule profile/(.*)$ profile.php?name=$1
My URL : http://localhost/profile/swatalk
Want to make : http://localhost/swatalk
If this is possible, Thank you all :)
RewriteRule ^profile/(.*) /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) /profile.php?name=$1 [L]
Line 1: (.*) matches any characters. it makes /profile/(.*) redirect to (.*) permanently.
Line 2: check if the request uri not matches any existing file
Line 3: check if the request uri not matches any existing directory
Line 4: then rewrite the request uri to /profile.php?name=$1, $1 is the backreference to (.*)
The above rule makes http://localhost/profile/swatalk redirect to http://localhost/swatalk, which is handled by /profile.php?name=swatalk
I need to add RewriteRule which works only if there is a certain string in the URL, otherwise I need to load the content from another folder. If the URL contains the string test, I need to send it to index.php as a parameter, else the content should be loaded from the directory folder.
For example: The root folder of the project is new_project. If the URL is http://localhost/new_project/test/something/, then I need to send test/something/ to index.php as parameter. Else if the URL is something like http://localhost/new_project/something/, then I need to load the content from directory/something folder.
Following is the .htaccess file I've written so far:
Options +FollowSymLinks
RewriteEngine on
# Force Trailing Slash
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
# Send request via index.php (again, not if its a real file or folder)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
# Serve other requests from the directory/ folder
RewriteRule ^(.*)$ directory/$1 [L]
What needs to be changed in the above .htaccess file so that the occurrence of test string in the URL passes the string after the string test along with the string test to index.php and if the URL doesn't contain the string test then loads the content from the directory folder?
You can test for the test/ inside the RewriteRule itself. Place the more specific rewrite rule before the "catch-all" directory/ rewrite rule.
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
# All requests starting with `test/` go to index (without the `test` part).
RewriteRule ^test/(.*)$ index.php?/$1 [L] # Not sure how GET parameters starting with `?/` behave.
# All others go to directory. Assuming not a valid file or dir.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ directory/$1 [L]
I know how to exclude folders, file types but i don't know how to exclude a link that contain a specific word.
I have this rule:
RewriteCond %{HTTP_HOST} ^www\.domain\.com
RewriteRule (.*) http://en.domain.com/$1 [QSA,L]
How to exclude from this rule the links that have at beginning: index.php?a=admin
This should do the trick:
RewriteCond %{HTTP_HOST} ^www\.domain\.com
RewriteCond %{REQUEST_FILENAME} index\.php
RewriteCond %{QUERY_STRING} !^a=admin
RewriteRule (.*) http://en.domain.com/$1 [QSA,L]
Here, we're adding a condition that states that the rewrite may only occur if the host is www.domain.com, the file being requested is index.php, and the query string does not being with a=admin.
So, www.domain.com/test will redirect to en.domain.com/test, but www.domain.com/index.php?a=admin will not redirect at all.
I've moved my abc folder from /myproduct/abc directory to /myproduct/extensions/abc on my server.
How can I redirect all calls to http://localhost/myproduct/abc to http://localhost/myproduct/extensions/abc ?
Ex : if requested URL is http://localhost/myproduct/abc/pqr.php, it should be redirected to http://localhost/myproduct/extensions/abc/pqr.php
Basically I want .htaccess code that can be placed inside /myproduct folder and if someone requests URL like http://localhost/myproduct/abc/pqr.php then it will look for occurrence of /abc/ and replace it with /extensions/abc/
We cannot replace /myproduct/abc/ by /myproduct/extensions/abc/ as myproduct can have white-labelled to yourproduct or myproduct1 etc..
Any help would be highly appreciated. :)
You may try this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /(.*)/abc/([^\.]+)\.php/? [NC]
RewriteRule .* %1/extensions/abc/%2.php [R=301,L]
Redirects
http://localhost/Any/Number/Of/Folders/abc/AnyFileName.php
To:
http://localhost/Any/Number/Of/Folders/extensions/abc/AnyFileName.php
To keep the first URL showing in the browser's addres bar, remove R=301 from [R=301,L]
NOTES:
If only folder myproduct/ is needed, like this: http://localhost/myproduct/abc/AnyFileName.php The rule will work too.
If file AnyFileName.php, pqr.php for example, exists at folder abc in the incoming URL, the rule will be skipped (Makes no sense to have it there anyway). The script has to be at folder abc in substitution URL: http://localhost/Any/Number/Of/Folders/extensions/abc/.
Vijay,
Try this:
RewriteEngine On
RewriteBase /
# REQUEST_FILENAME should not be file name
RewriteCond %{REQUEST_FILENAME} !-f
# REQUEST_FILENAME should not be directory name
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /(.*)/abc/(.*)/? [NC]
RewriteRule .* %1/extensions/abc/%2 [R=301,L]
Which server are you using? Have a look at mod_rewrite in case you are using Apache 2.
http://httpd.apache.org/docs/current/mod/mod_rewrite.html