I'm trying to wrap my tiny brain around how the .htaccess can convert my somewhat undesirable URL into a cleaner eye candy link.
This is my current, scruffy URL.
expand.php?category=Mods&subcategory=Wrestlers&faction=WWE
And what I would like it to be render as, would be something perhaps like this?
expand/Mods/Wrestlers/WWE/
Am I right in thinking this is correct syntax to perform this? Because it doesn't seem to do anything right now!
RewriteRule ^([^/]+)/([^/]+)$ expand.php?p=$1&sp=$2 [L]
I would appreciate it if some bright chap might be able to help me out of this pickle!
You can do it like this. You will need to make sure the rewrite matches the URL you wish to internally redirect too, which is expand.php?category=Mods&subcategory=Wrestlers&faction=WWE
So to take care of that you should be able to use this.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^expand/([^/]+)/([^/]+)/([^/]+) expand.php?category=$1&subcategory=$2&faction=$3 [NC,L]
Try
^expand/(\w+)/(\w+)/(\w+)?$ expand.php?category=$1&subcategory=$2&faction=$3
To internally rewrite expand/Mods/Wrestlers/WWE/ to expand.php?category=Mods&subcategory=Wrestlers&faction=WWE, you'll need to make a regex that matches that first url. Your current regex would match an url with 2 parts, but your example url has 4 parts. I am pretty sure that doesn't fit ;-)
So... how do we fix it? Well, we make a regex with 4 parts. In fact, we know that the first part needs to be equal to expand, so we end up with this:
RewriteRule ^expand/([^/]+)/([^/]+)/([^/]+)/?$ /expand.php?category=$1&subcategory=$2&faction=$3 [QSA,L]
If you add this to the .htaccess in your www-root and we now go to http://example.com/expand/Mods/Wrestlers/WWE/, we should see whatever expand.php outputs with those parameters.
Related
A few minutes ago I asked a question and received a very good response. It works. (This is it). I added it to my PHP scripts. Now I need to complete my site with another question like that but little characterized. I need to get rewrite rule from that regex (it's different problem, not my previous).
I need to rewrite link like:
http://my_site.com/technic/k-700/?type=repair
to link like:
http://my_site.com/repair/k-700/
Instead of k-700 can be any another combination (between / ) and instead of repair can be only kit.
Now I need rewrite rule for .htaccess file. Please.
My result is not working:
RewriteRule ^([^\/].)/k-700/$ /technic/k-700/?type=$1 [L]
I can't avoid that k-700 :(
If my understanding is correct and this is going in your root .htaccess, then try this:
RewriteRule ^(repair|kit)/([^/]+)/?$ technic/$2/?type=$1 [L]
UPDATE: Re-read your description. a little ambiguity with the repair vs kit value and whether substitution should always have k-700. So here are a few more examples catering to those possibilities:
RewriteRule ^(repair|kit)/([^/]+)/?$ technic/k-700/?type=$1 [L]
RewriteRule ^(kit)/([^/]+)/?$ technic/k-700/?type=$1 [L]
RewriteRule ^(kit)/([^/]+)/?$ technic/$2/?type=$1 [L]
I have a url like this :
www.qwerty.in/details.php?vendor_id=1&name=abcd%20cafe
I am trying to conver this like below with the help of .htaccess file :
www.qwerty.in/1/abcd-cafe
I am trying the below but unfortunately its not working . can anyone help
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ details.php?vendor_id=$1&name=$2
You character must allow space as well:
RewriteRule ^([\w-]+)/([\w-\s]+)$ details.php?vendor_id=$1&name=$2 [L,QSA]
I have used \w, which is same as [a-zA-Z0-9_].
The following rule will convert your URL from www.qwerty.in/details.php?vendor_id=1&name=abcd%20cafe to www.qwerty.in/1/abcd%20cafe:
RewriteRule ^([^/]*)/([^/]*)\.html$ /details.php?vendor_id=$1&name=$2 [L]
It it much more generalized however, and will convert the first two parameter values even if they are not vendor_id or name. If this is not the behavior you'd prefer, have a look at anubhava's answer.
Converting a space to underscore can be achieved using:
RewriteRule ^(.*)\s(.*)$ $1_$2 [N]
Also, don't forget to ensure the rewrite engine is online:
RewriteEngine On
If you want to further experiment with these rules, I'd recommend a website I recently discovered: http://www.generateit.net/mod-rewrite/
Currently I have a rule in the .htaccess file that makes a shorter URL:
RewriteRule ^job(.*)$ /include/myfile.incl.php?proj=$1 [NC,L]
to make the final URL looks like:
mydomain.com/jobXXXXXX
Is it possible to accommodate second query is a user changes my URL like this?
mydomain.com/jobXXXXXX?token=123
I need to be able to pick-up that token value too then.
Perhaps there's a way to add a conditional $2 somehow? I've tried QSA flag but it did not work for me. Perhaps my Apache version is too old... 2.2.3(CentOS)
Whichever solution I could use, another rewrite rule or perhaps in combination with PHP. I'm stuck.
UPDATE
NM... I think the problem is in my PHP, not .htaccess
Are you talking about QSA flag?
RewriteRule ^job(.*)$ /include/myfile.incl.php?proj=$1 [QSA,NC,L]
The QSA flag should work. I'm using it 5 years now.
Use %{QUERY_STRING} in your rewrite target to access the query string. Something like:
RewriteRule ^job(.*)$ /include/myfile.incl.php?proj=$1&%{QUERY_STRING} [NC,L]
Trying to write a rewrite rule to capture two GET variables
http://stackoverflow.com/blogs/category/general/1
RewriteRule ^blogs/category/(.+)/?$ blogs.php?category=$1 [PT,L,QSA]
RewriteRule ^blogs/category/(.+)/([0-9]+)/?$ blogs.php?category=$1&page=$2 [PT,L,QSA]
However when I grab these from the headers it looks like this?
$_GET['category'] = "general/1";
$_GET['page'] = "";
As you can see I have two rules, one for just when they provide category and one for when they also provide page number. Might be wrong about that approach I'm not sure.
What am I doing wrong here? How can I separate these variables properly using the rewrite rules (I know I could hack it in php but that's ugly)
I think you just have to switch them so that the more specific one is handled first:
RewriteRule ^blogs/category/(.+)/([0-9]+)/?$ blogs.php?category=$1&page=$2 [PT,L,QSA]
RewriteRule ^blogs/category/(.+)/?$ blogs.php?category=$1 [PT,L,QSA]
To explain that a little bit further: All regular expressions are greedy if not specified otherwise. Which means, that the regular expression tries to get as much as possible. (.+) matches "general/1".
I have the following rule:
RewriteRule ^(image/[0-9]*/(.*))?$ image.php?prettyUrl=true&nav=$1 [QSA,NC,L]
This is supposed to replace
http://mysite.com/image/12345/imagename.png => http://mysite.com/image.php?pretyUrl=true&nav=image/12345/imagename.png
For the particular case where the pretty url contains '/image/' the $_GET array is empty. If I use a different name like 'images' or 'asdfg' the $_GET array will contain both pretyUrl and nav.
Any idea why?
Thanks!
If the rewrite rule must work for every URL in image/, (and the /1234/ digits part is not required), then this may be what you are looking for:
RewriteRule ^(image(/.*)?)$ image.php?prettyUrl=true&nav=$1 [QSA,NC,L]
If you can phrase your question a bit more specifically, a better solution can be refined.