URL change with .htaccess RewriteRule - .htaccess

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

Related

hteaccess: Pretty url by htaccess showing 404 error

I am beginner in codding. I want make url pretty. I search this question on google then i found i have to use .htaccess. I tried htaccess code.
Here is my URL looklike
website.com/project-details.php?Residential/The-Serenas/1
I want to make url like website.com/Residential/The-Serenas/1
I want to remove entire "project-details.php?" word from url by htaccess.
here is my .htaccess code
Options +FollowSymLinks
RewriteEngine On
RewriteBase /basename/
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /project-details\.php\?([^\s&]+) [NC]
RewriteRule ^ view/%1? [R=302,L]
RewriteCond %{THE_REQUEST} /project-details\?([^\s&]+) [NC]
RewriteRule ^ view/%1? [R=302,L]
# internally rewrites /view/ABC123 to project-details.php?ABC123
RewriteRule ^view/([A-Za-z0-9_#./#&+-]+)$ project-details.php?$1 [L,NC,QSA]
# PHP hiding rule
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
Above htaccess code work and url replacce to website.com/view/Residential/The-Serenas/1
If i remove view from this line RewriteRule ^ view/%1? [R=302,L].
Then page goes to 404 error. If i dont remove view then code worked but view show on url.
I dont want to show anything on url only remove "project-details.php?" this.
This rule is to check the request uri whether is /project-details.php and any query string follow behind, and redirect user to the uri that matches the query string. %1 is the backreference and provide access to the grouped parts (in parentheses) of the pattern, in this case ([^\s&]+). For example, if uri /project-details.php?Residential/The-Serenas/1 is requested, the user will be redirected to /Residential/The-Serenas/1.
RewriteCond %{THE_REQUEST} /project-details\.php\?([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L]
This rule is to check if user requests existing file and directory, if not, then the uri is rewritten internally (only server knows the destination uri, user does not know this). For example, if uri /Residential/The-Serenas/1 is requested, the server will rewrite the uri to project-details.php?/Residential/The-Serenas/1.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Za-z0-9_#./#&+-]+)$ project-details.php?$1 [L,NC,QSA]

Rewrite URL to remove query string

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

htaccess Rewrite rule not working as I expected

I have the following rule:
RewriteRule ^(.*)$ index.php?ip=$1 [L]
But it's not working, instead it is loading:
https://example.com/?ip=index.php
Am I missing something?
Your rule is looping because there is no condition to stop rewriting for existing files and directories.
After first rewrite it becomes:
index.php?id=1.2.3.4
and after second rewrite URI becomes:
index.php?id=index.php
You can use this rule to fix this behavior:
# 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 ^(.*)$ index.php?ip=$1 [L,QSA]

Rewrite Rule Loop. How to stop applying rules

I have an .htaccess file redirecting blog post urls like /blog/2012/11/30/this-post to /blog/post.php?id=this-post. Works fine until I apply a second rule below it that is also a match. This rule is set to take a path formatted url like /this/is/a/pageid and redirect to /page.php?id=pageid. It doesn't care how long the path is, it just uses the last directory in the path as the id. Unfortunately, this rule matches everything and I'm not sure how to stop the redirect after the first match. Here is my .htacess
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/([0-9]+)/([0-9]+)/([0-9]+)/([A-Za-z0-9-]+)$ /blog/post.php?id=$4 [L]
RewriteRule ([^/]+)/?$ /page.php?id=$1 [L]
Thanks in advance for the help.
You can add an optional query parameter after you redirect the first time and combine that with a simple rewrite condition on the query string to check if that parameter exists or no, try this :
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/([0-9]+)/([0-9]+)/([0-9]+)/([A-Za-z0-9-]+)$ /blog/post.php?id=$4 [L]
RewriteCond %{QUERY_STRING} !^(.*&)?r=0(&.*)?$
RewriteRule ([^/]+)/?$ /page.php?id=$1&r=0 [L]

htaccess wildcard redirect to query string

Quick question, and I've seen it asked hundreds of times, but I just can't seem to get it to work (shame on me).
I'm trying to redirect anything other than index.php to view.php?id=$1 where $1 is anything other than index.php - but I can't seem to get it to work.
For example:
http://domain.com/ should use index.php
http://domain.com/index.php as should this
but..
http://domain.com/sdgoi3 should use http://domain.com/view.php?id=sdgoi3 etc
I've tried a few things and gone down through the questions above but to no avail.
Anyone got a solution? Appreciated.
Try putting this in the htaccess file in your document root:
RewriteEngine On
RewriteRule ^$ /index.php [L]
RewriteRule ^index\.php - [L]
RewriteRule ^view\.php - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /view.php?id=$1 [L]
The logic here is:
if the request URI is / rewrite to index.php
if the request URI starts with index.php, don't change and pass through
if the request URI starts with view.php, don't change and pass through
if the request is to a non-existing file or directory, pass to view.php with the id param
Maybe something like :
RewriteCond %{REQUEST_URI} !^index\.php
RewriteCond %{REQUEST_URI} !^view\.php
RewriteRule ^(.*)$ view.php?id=$1 [L]
?

Resources