creating a default variable in mod_rewrite - .htaccess

I have mod_rewrite take this url:
mydomain.com/directory/index.php?slug=slug&year=2009
and make it pretty:
mydomain.com/directory/slug/2009/
Easy, but now my issue is, if someone lands at the URL without the year attached (like: mydomain.com/directory/slug/), how can I add the current year to the URL?
My current htaccess reads:
RewriteEngine on
RewriteRule ^([^/\.]+)/([^/\.]+)?$ /directory/$1/$2/ [R]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ /directory/index.php?slug=$1&year=$2 [L]

Try using server variables:
RewriteRule ^([^/\.]+)/?$ /directory/$1/%{TIME_YEAR}/ [R]
If I didn't make a mistake this should redirect url:
mydomain.com/directory/slug/
to:
mydomain.com/directory/slug/2009/
(for another 2 and half months ;) )
I think you now what to do, if you don't want redirect or / at the end of url :)
I tried this in similar case, and this worked fine.
Oh, and you can find list of server variables in mod_rewrite documentation. I am not sure if this will always work.

Related

.htaccess Rewrite rule doesn't work

I know there are already a ton of these questions but I don't get it...
I'm trying to rewrite my /register.php to /register but I can't.
.htaccess code
RewriteEngine on
RewriteBase /
RewriteRule ^register$ register.php [L]
Does any of you know the answer?
Sorry for the stupid question but I'm kinda new to the whole .htaccess stuff...
Do you have enabled the Apache module mod_rewrite?
I use this pattern to create user-friendly URLs. It's pretty variable. It means, that every address in format letters-numbers with a certain file extension (PHP or HTML) will be translate into letters-numbers.php (for example).
RewriteRule ^([a-z]+)-([0-9]+)\.(php|html)$ /$1-$2.$3 [L]

.htaccess url rewrite rule to subdirectory not working

I want this: RewriteRule ^paper$ /express/index.php?c=p [L] but it does not work. Instead, it's trying to send me to /index.php?c=p, ignoring the subdirectory express altogether. However, RewriteRule ^express/paper$ /express/index.php?c=p [L] works. This is my .htaccess code:
RewriteEngine on
RewriteBase /
RewriteRule ^paper$ /express/index.php?c=p [L]
RewriteRule ^express/paper$ /express/index.php?c=p [L]
The url I WANT is just http://site.com/paper but I have to do http://site.com/express/paper to get the rewrite rule to work.
And yes, I only use one of those rewrite rules at a time.
UPDATE
Well, I'm not really getting any responses, so I'm going to go ahead and close this browser tab. I'll check back now and then but don't be offended if it takes a little while. Thanks for any help offered.

How to remove middle part from URL - mod_rewrite

How to rewrite this url "www.domain.com/index.php?route=custom/static/page" to "www.domain.com/page" in htaccess file, basically just want to take out index.php?route=custom/static/ from urls.
I don't know regex so I tried http://www.generateit.net/mod-rewrite/, but it only generates
RewriteEngine On
RewriteRule ^([^/]*)$ /index.php?route=$1 [L]
which doesnt remove 'custom/static' from URLs, I tried a few other examples as well but only removes index.php? and doesnt pass variable, any help is appreciated.
Do you know the concept of using mod-rewrite?
In your question you have mentioned to use mod-rewrite to redirect
"www.domain.com/index.php?route=custom/static/page",
Here $_Get['route']="custom/static/page"] $url_parameter=$_Get['route']
to
"www.domain.com/page" [here $_Get['route']="page"],
So now you can mannually add "custom/static/" to the obtained value of $_Get['route']. as $url_parameter="custom/static"+$_Get['route'] //For PHP
Using your mod_rewrite you can fulfill your demands,
RewriteEngine On
RewriteRule ^([^/]*)$ /index.php?route=$1 [L]
But if you need out of box solution using .htaccess then I suggest learning "rewrite-engine" instead of using generating tool

How do I get a url to use pretty urls? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Url Rewriting Help
How do I get a url ending in /index.php?p=2 to display as /2 instead using .htaccess (pretty urls)?
I currently am working on a webcomic displaying site but I have run into a problem with my .htaccess file. I am trying to rewrite /index.php?p=2 to /2 but for some reason it is not working.
So yeah any help would be much appreciated!
.htaccess can't rewrite /index.php?p=2 into /2. You are approaching the problem in reverse.
Rewrite rules tells Apache to do something when fetching a specific URI. What you really need to do is tell Apache to "rewrite" a request to /2 so that it fetches /index.php?p=2 instead. You can do so with the following RewriteRule:
RewriteRule ^([0-9]+)$ index.php?p=$1 [L]
and then change, in your HTML, every single link to point to the rewritten URI instead of the canonical one. Apache will not "rewrite" your links for you. It will however, with the help of the rule above, fetch the proper resources when the client queries for /2.
Make an .htaccess file similar to this one... (the flags may need to be modified/varied per mod rewrite flag documentation: http://httpd.apache.org/docs/2.3/rewrite/flags.html)
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([A-Za-z_0-9-]+)/?$ index.php?p=$1 [QSA,L]

Apache / Linux - .htaccess how to get specific variable from a query / url and apply to rewrite

I have a rule that works for one "direction" but, not the other.
A typical incoming url / query would be: (long url)
http://somedomain.com/getme.pl?dothis=display&partnum=1234567 (could be up to 9 digits)
I have this rule in place in my htaccess file:
RewriteRule ^([0-9]*)$ /getme.pl?dothis=display&partnum=$1 [L]
Which works great for a bit of ease getting one of the unique part numbers:
http://somedomain.com/1234567.
However, I would like to make the long url "pretty" so, I assumed I could reverse(ish) it.
So, when a link on the site is clicked on (the long url) the htaccess file would process the long url to the beautified version.
I tried MANY attempts.
Here was my latest failure.
RewriteRule ^([0-9]*)$ /getme.pl?dothis=display&partnum=$1 [L] #(works)
RewriteCond %{QUERY_STRING} ^partnum=([0-9]*) #(tried to get partnum)
RewriteRule ^.* http://%{HTTP_HOST}/%1 [R] #(make the short url)
RewriteRule ^([0-9]*)$ /getme.pl?dothis=display&partnum=$1 [L] #(the known working rule)
I have tried a plethora of rules and visited many sites for advice.
I tried with just rules, just conditions and variations of query_string.
So, I believe I must just grab the "partnum" from the query and rewrite to /1234567 or http_host/1234567
Then, allow the other rule (works) to process.
So BOTH:
http://somedomain.com/getme.pl?dothis=display&partnum=1234567
and
http://somedomain.com/1234567
Display as: http://somedomain.com/1234567 in the browser.
and both passed the whole query to the getme.pl script properly.
I found some close answers here but, none that really explained what I needed.
Can someone please help?
From the sounds of it, this should get you moving down the right path:
# Your working rewrite, with extra param on the rewrite
RewriteRule ^([0-9]*)$ /getme.pl?dothis=display&partnum=$1&rewrite [L]
# Redirect for long urls, to pretty url
# -The appended '&rewrite' on the first rule will force this not to match
# -The trailing '?' on the rewrite will strip the query string
RewriteCond %{QUERY_STRING} partnum=([0-9]*)$
RewriteRule (.*) /%1? [L,R=301]
Hope that helps.

Resources