I am currently using .htaccess to rewrite my urls from
example.com/mixtape.php?id=(WHATEVER #)
...to...
example.com/m/(WHATEVER #)
The code i use to accomplish that is below:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^m\/([^/\.]+)/?$ mixtape.php?mixid=$1 [L]
The next thing I am trying to achieve, in a clean matter, is to rewrite my
example.com/edit-mixtape.php?id=(WHATEVER #)
...to...
example.com/m/(WHATEVER #)/edit
EDIT: the "WHATEVER #" is the ID of the Mixtape I am editing. Normally i will use "$_GET['id']" to see what mixtape i'm referring to and then i fetch everything related to that number.
IS there anyone out there that would be able to help me successfully write a proper re-write mod?
Based on your comment, I would change your original rule as well to allow only for numbers:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^m/(\d+)/edit/?$ edit-mixtape.php?mixid=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^m/(\d+)/?$ mixtape.php?mixid=$1 [L]
By the way, I would probably use a general rule and write both urls to the same php file and handle the action in the controller.
You can use the same rules that you had before but just append an edit to the regular expression:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^m\/([^/\.]+)/edit/?$ edit-mixtape.php?mixid=$1 [L]
Note that your original rules have the query string mixid= while your examples say id=.
Related
i try use RewriteRule in htaccess
i want my url
site.com/f_search.php?langs=en&langs=en
works like
site.com/en/new.php?/en/f_search
i used this code in htaccess
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?/new.php+?+/([^/]*)/?/f_search/?$ f_search.php?langs=$1&langs=$2
but its not work with " ? "
if i change new.php+?+ to new.php+#+
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?/new.php+#+/([^/]*)/?/f_search/?$ f_search.php?langs=$1&langs=$2
it works good
site.com/en/new.php#/en/f_search
any idea to change # to ? in my url ???
thanks
The question mark and what follows is not part of the subject the pattern of a RewriteRule is matched against. That is clearly documented. Instead you need to use a RewriteCond to access the content of the query string.
I assume this is roughly what you are looking for, though you might have to tweak it to match your actual situation:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^/([a-z]+)/f_search$
RewriteRule ^/?(?:([a-z]+)/)?new\.php$ /f_search.php?langs=$1&langs=%1
From your question it is unclear where the two values for the argument "langs" in the rewrite target should get taken. So you may have to adjust that to your needs in the rule.
Also it is puzzling that you are trying to set that argument "langs" twice ... That makes little sense, the second attempt will simply overwrite the value of the first attempt when those values are handed over to your php based logic.
The documentation of the rewriting module for the apache http server offer a lot of precise help and good examples: https://httpd.apache.org/docs/current/mod/mod_rewrite.html
the final code
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([a-z]+)/new.php$ f_search.php?langs=$1 [L,NC]
I am passing a name which will be used as the subfolder but the filename remains the same eg foldername/gallery.php?q=new_folder should be new_folder/gallery.php using htaccess.
Attaching the htaccess of what I a trying to do
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(gallery.php)$ $2.php?q=$1 [L]
gallery.php is the filename and $1 is basically the new_folder. Been struggling with this for quite a bit.
You are fairly close. You just need to use foldername/ in target URI of your rule.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(gallery\.php)$ foldername/$2.php?q=$1 [L,QSA,NC]
I have just added bellow mentioned code in .htaccess. Hope this will help you.
RewriteEngine On
RewriteRule ^foldername/(.*) http://example.com/newfolder/gallery.php?options=$1 [R=301,L]
Hi there I'm trying to make a blog demo and I'm having some pretty URL codes already.
i am having a url www.xyz.com
and a search url www.xyz.com/search/this+is+a+search+text
in search url the parameter search is a page name and this+is+a+search+text is a parameter that i'll be parsing
I'm having a .htaccess code below already
# code to make pretty URLS | we're using this code to achieve /category/slug
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.+)/([\w-]+)/([\d]+)$ app/post.php?&category=$2&page=$3 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.+)/([\w-]+)/([\w-]+)$ app/post.php?&category=$2&slug=$3 [L,QSA]
# code to make pretty URLS for search page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.+)/([\w-]+)/([\d]+)$ app/search.php?&searchstring=$2&page=$3 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.+)/([\d]+)$ app/index.php?page=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.+)/([\w-]+)$ app/post.php?category=$2 [L]
I'm using the below code for serach page
# code to make pretty URLS for search page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.+)/([\w-]+)/([\d]+)$ app/search.php&searchstring=$2&page=$3 [L,QSA]
But while using the code I get a 500 internal error message, I'm not able to figure out what's the error!
I would really appreciate if anyone could help me out with this logic.
Rewrites in achieve /category/slug and for search page are the same. Search request matched with first - achieve rewrite and run app/post.php...
To answer your followup question to JarekBaran
JarekBaran: Rewrites in achieve /category/slug and for search page are the same.
What this means is that the RewriteCond are the same for the pretty URL code for the caterogy/slug page and the search page.
This means that the first match will always be used and so:
RewriteRule ^/?(.+)/([\w-]+)/([\d]+)$ app/post.php?&category=$2&page=$3 [L,QSA]
will always be triggered before
RewriteRule ^/?(.+)/([\w-]+)/([\d]+)$ app/search.php?&searchstring=$2&page=$3 [L,QSA]
This way you will not be able to use the search function. You could consider adding a parameter to identify if a search is made or not.
Your problem is that you are starting your rules with (.+) which will match one or more of anything. This means that you are also matching the / character. Some of your 2 parameter redirects are matching your urls with 3 parameters because of this. You would be better of starting with something like this - ([a-zA-Z0-9-]+)
Your search rewrite should probably be something like this - RewriteRule ^/?([a-zA-Z0-9-]+)/([\w+]+)/([\d]+)$ app/search.php?&searchstring=$2&page=$3 [L,QSA]
I tested that rewrite on this htaccess testing tool and it is working.
That said your other rules will need to be changed as well since they all start with (.+). After doing that your category rewrite will conflict with your search rewrite.
However, this would probably work for you:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?category/([\w-]+)/([\d]+)$ app/post.php?&category=$2&page=$3 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?category/([\w-]+)/([\w-]+)$ app/post.php?&category=$2&slug=$3 [L,QSA]
# code to make pretty URLS for search page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?search/([\w+]+)/([\d]+)$ app/search.php?&searchstring=$2&page=$3 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([a-zA-Z0-9-]+)/([\d]+)$ app/index.php?page=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?category/([\w-]+)$ app/post.php?category=$2 [L]
I have enabled AllowOverride All in Apache configuration file.
This the URL users are typing in browser
http://hostname/system_name/dynamic_text/login
This needs to be working as below
http://hostname/system_name/index.php?id=dynamic_text
I have created rewrite rule as below. But its not working.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z])$/login index.php?login=$1 [NC,L]
Can you suggest better way to fulfill this requirement
Problem is with your regex pattern as [a-z] only matches a single lowercase letter. You can use this rule:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/login/?$ index.php?login=$1 [NC,L,QSA]
This is created as per answers and comments above and works for me as expected.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/login/?$ index.php?login=$1 [QSA,NC,L]
I'm no regex expert and I'm trying to implement on a website a social login system that may or may not redirect the user to the page where he was before loggin in.
Like so, for no redirection:
RewriteRule ^login/([^/]+)$ login.php?p=$1 [L]
http://example.com/login/Facebook >> the page does it stuff and goes to index (default behaviour).
or like this for redirection (p = provider; r= redirect relative path):
RewriteRule ^login/([^/]+)/([^/]+)$ login.php?p=$1&r=$2 [L]
Works great if the user is on a page like http://example.com/info that is generated by the following rule:
RewriteRule ^info$ users_infosystem.php [L]
I'm having problem when the user is on a page like http://example.com/gallery/galleryname
RewriteRule ^gallery/([^/]+)$ galery.php?name=$1 [L]
Any Help?
EDIT: Just occured me ... I don't know if the $ char is the delimeter that marks the end and taking it off makes the rule accept everythings that is after the ([^/]+) bit.
Also: Doesn't the ([^/]+) bit match everything but a forward slash?
I'm having problem when the user is on a page like
That is a very vague sentence. You don't actually say what the problem is. What about your rule is not working? Also didn't paste your entire htaccess file, you pasted bits and pieces so we don't even know what order your rules are in or if there are other rules.
I don't see an issue with these rules. These rules should work. They all match something different. Also you can make the / optional using the ? after it.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^login/([^/]+)/([^/]+)/?$ login.php?p=$1&r=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^login/([^/]+)/?$ login.php?p=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^info/?$ users_infosystem.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^gallery/([^/]+)/?$ galery.php?name=$1 [L]
The last rule will match http://example.com/gallery/galleryname with or without a / at the end.