RewriteEngine On
RewriteRule ^(register)/?$
RewriteRule ^([\w-]+)/?$ profile.php?username=$1
On the second line, I would not like to intercept or redirect the user. If the url matches the given regex pattern, I would like to perform no operation and the url user requested should go where it was intended to. How do I do this?
Since you want to redirect all requests to profile.php except for register, you can add an additional rewrite condition to only redirect if request URI is not register.
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/register/?$ [NC]
RewriteRule ^([\w-]+)/?$ profile.php?username=$1
Demo: https://htaccess.madewithlove.be?share=5f7d67b4-a374-5195-975e-a9742a685fdf
Related
Redirect If url contains particular word without changing it.
eg: http://localhost/APIs/student
should redirect to "http://localhost/APIs/"
I tried many things one of them given bellow:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^[A-Z]{10,}\s/+index\.php\?page=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteRule ^([a-z0-9-]+)/?$ /APIs/index.php?page=$1-10 [QSA,L,NC]
above snippet fails if I pass more than one parameter in the link after slash.
Here are the rules
RewriteEngine on
RewriteRule ^APIs/(.*)$ APIs/index.php?page=$1 [NC,END]
If you request for
http://localhost/APIs/student/test
you will be internally redirected to (you will see the same url as above)
http://localhost/APIs/index.php?page=student/test
In the index.php file use $_GET["page"] to get student/test and you can decide what to show in the page based on this.
On this other topic : .htaccess redirect according to PHP parameters I got help to make redirections according to some PHP parameters, for instance, like
/forum/viewtopic.php?t=123 redirected to /page1.html
/forum/viewtopic.php?t=345 redirected to /page7.html
/forum/viewtopic.php?t=89 redirected to page3.html
The (working) solution proposed is
RewriteEngine On
RewriteCond %{QUERY_STRING} ^t=123$
RewriteRule ^forum/viewtopic\.php$ /page1.html? [L,R=301]
RewriteCond %{QUERY_STRING} ^t=345$
RewriteRule ^forum/viewtopic\.php$ /page7.html? [L,R=301]
RewriteCond %{QUERY_STRING} ^t=89$
RewriteRule ^forum/viewtopic\.php$ /page3.html? [L,R=301]
I would like to add a "general" case at the end: if no URL matched, and the folder "/forum" is asked, redirect to the root. For instance
/forum redirected to /
/forum/viewtopic.php?t=34598237 redirected to /
/forum/something redirected to /
How can I do that ?
Just add this line at he end:
RewriteRule ^forum / [L,R=301]
Since all of your previous conditions had [L] modifier (which instructs any further rules processing), this last condition would only kick in only if any of the previous ones does not match.
Currently I set a rewrite rule like so, to produce clean and simple url's.
.htaccess
RewriteRule ^/about$ about.php [L]
But what i need to do is something a little different, the other way around. For example if a user clicks the following link
about
They would go to the about page /about
Currently the about page resides at index.php?a=about&b=user and this can't be changed unfortunately. As you can see it does not look very nice in the browser address bar.
EDITED
I am using a pre-made script from phpdolphin, which is working fine. But the url are all index.php based and i would like to clean them up
Currently the only code within the .htaccess file
RewriteEngine on
RewriteCond %{request_filename} -f
RewriteRule ^(.*) $1 [L]
RewriteRule ^(([^/]*)+)(/([^/]{0,32})(/.+)?)?$ index.php?a=$1&q=$3 [L]
Add this rule before your existing rule:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?a=([^&]+)&b=user[\s&] [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteRule ^([^/.]+)/?$ index.php?a=$1&b=user [L,NC,QSA]
You can add this RewriteRule to redirect the request when user hit index.php?a=about&b=user
RewriteCond %{QUERY_STRING} ^(.*)a=about(.*)$ [NC]
RewriteRule ^index.php /about [L,NC,R=301]
or you can use php header() function in index.php to redirect the request:
if ($_REQUEST['a'] == about) {
header('Location: /about');
exit;
}
Try this i checked and it works ...
RewriteEngine On
RewriteRule ^([A-Za-z0-9-+_%*?]+)/([A-Za-z0-9-+_%*?]+)/?$ index.php?a=$1&q=$2 [L]
Note: add additional signs between [] if necessary
OR This
RewriteRule ^([^~]+)/([^~]+)/?$ index.php?a=$1&q=$2
I like to use this code because it's short and matches everything except for ~ which is very very rare to see in a url
i make this in htaccess file .. for rewrite url from different page
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?username=$1 [QSA]
RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?username=$1 [QSA]
and it's works fine ..
now i need to add more as facebook
you can put facebook.com/username
which is : facebook.com/profile.php?username=
& you can put facebook.com/pagename
which is : facebook.com/pages.php?username=
and works fine
from different pages ..
I need to make like this ..
any help ?!
RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?username=$1 [QSA]
RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?username=$1 [QSA]
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?username=$1 [QSA]
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?username=$1 [QSA]
Your regex patterns match identically, so you're going to need something to differentiate between pages and people (or redirection to profile.php vs. index.php).
You can accomplish this one of two ways. First, you can go all to one page, let's call it redirect.php. On this page, you could check the id value and then redirect. So like:
RewriteRule ^([a-zA-Z0-9_-]+)/?$ redirect.php?id=$1
In redirect.php, you grab $_GET['id'] and run some query against it to determine if it's a page or person, and then redirect using something like:
header("Location: profile.php?username=".$_GET['id']);
If you strictly looking for Facebook related stuff, you can hit the graph API up instead of the database to determine if its a page or person ID. (Not sure if it can exist in both - but I think not).
If you don't have a database or data source to determine if its a page or person, then you'll need to add something to your URL strings to determine, like /pages/{pageid}, which you could then rewrite your rules to:
RewriteRule pages/([a-zA-Z0-9_-]+)/?$ index.php?username=$1 # for pages
RewriteRule ^([a-zA-Z0-9_-]+)/?$ profile.php?username=$1 # for people
If you can't use something in your URL and don't have a way to access it via database, then you're out of luck, as you're hitting two identical patterns.
While writing a rewrite rule specifically for only a particular URL to remove a trailing slash from it, an infinite redirect loop is occuring.
The following is the code I am trying to code in .htaccess:
RewriteRule ^abc.php /abc/ [R=301,L,NC,QSA]
RewriteRule ^abc/ abc.php [NC,QSA]
I am trying to make a 301 Redirect on abc.php to abc/ but want to serve abc/ with abc.php's content only. Want to write a Rule specifically for this url only.
You can break the infinite loop if you check the HTTP request line (see here and also here) sent to the server by the browser with a RewriteCond and, in addition, you reverse the order of the rules:
RewriteRule ^abc/ abc.php [L,QSA]
RewriteCond %{THE_REQUEST} ^GET\ /abc\.php
RewriteRule ^abc.php$ /abc/ [R=301, QSA]
Try this :
RewriteEngine On
RewriteRule ^abc/? abc.php [NC,L,QSA]
RewriteRule ^abc.php /abc/ [R=301,QSA]
Hope it helps!