I want to redirect domain.com/1/file.php to domain.com/2/file.php and domain.com/1/pic.jpg to domain.com/2/pic.jpg etc. I want to redirect the subdirectory while leaving to end file in tact. I'm finding it hard to look for a tutorial as I can't describe my problem easily in google. Could someone show me how to do this or direct me to a tutorial about it.
Thanks
RewriteEngine On
RewriteRule ^([0-9])/([a-zA-Z]+).php$ 2/$2.php
Using mod_rewrite...You can do the same thing for your images as well. Essentially the regex expression will collect the value for a backreference. so $1 and $2 since i put 2 regex expressions in there.
Of course, if you need a different match instead of domain.com/1/... you will need to have a regex pattern that matches (but you can just google the appropriate regex expressions you may need but i took your example literally)
Try following for redirect. Replace 1 and 2 with respective subdirectories.
RewriteEngine On
RewriteRule ^1/(.*\.)(jpg|php) /2/$1$2 [L,NC,R=302]
Related
I want to change URL format from :
https://example.com/modules/news/article.php?storyid=224039
to :
https://example.com/news/224039
Any one can help to write true .htaccess codes?
thanks
Untested:
RewriteEngine On
RewriteBase /
RewriteRule ^news/([0-9]+) /modules/news/article.php?storyid=$1 [NC,L]
The NC flag is for No Case, if you want case insensitivity. If not, remove this flag. The L is the Last flag, meaning it would be the last rule parsed in the given rewrite instance so further rewrites aren't used. This is a bit counterintuitive in the sense that Apache will re-read all the rules all over again from the beginning anyways after the rewrite to make sure it doesn't have to rewrite again, and is a gotcha for many people regarding infinite rewrite loops... Probably can also omit the L flag altogether, but is more expressive.
The RewriteEngine On can be omitted in Apache configurations that enable this in the httpd.conf file. It is best practice to put it on again before assuming the engine is on. The rewrite base / probably can be omitted, depends on how you write your RewriteRule. Finally the RewriteRule uses a regular expression on the left, the parenthesis stores the match, the brackets define a character list, 0-9 is the valid characters, could also use \d instead, the + means match 1 or more times. The expression on the right is what to replace it with. The leading slash can probably be omitted. Also note that due to the presence of a querystring on the right side, if a querystring was present on the left side, it will be discarded. If you want to merge query strings, use the QSA flag meaning querystring append, and then it will merge querystrings when adding your storyid. Finally the $1 means use the first match that was captured with parenthesis on the left.
Currently I have my htaccess configured so that when I type anything after the domain, it is treated as a get
RewriteRule ^(.*)$ /index.php?id=$1
so
www.example.com/test
will redirect to
www.example.com/index.php?id=test
now what I would like is the page to detect if after the first / there is the # character and do something diffirent
for example,
www.example.com/#test
goes to
www.example.com/index.php?abc=test
whilst still retaining the first rule, can this be done?
And as a bonus, if you know how to use the # symbol instead of the #, please do let me know, I tried putting NE flag in my rule but I had no luck.
I need to say - this directory and anything after do this.
/thedirectory/* How would I write this?
Also I'm trying to figure out what exactly /|$ means. And what !^ means.
RewriteCond %{REQUEST_URI} !^/thepage(/|$)
Can someone help me with this? Researching trying to find the answer to my questions isn't coming up with answers.
This is about RewriteCond and regular expressions
To answer !^ first, it is two things ! (RewriteCond)
CondPattern is usually a perl compatible regular expression, but there is additional syntax available to perform other useful tests against the Teststring:
1. You can prefix the pattern string with a '!' character (exclamation mark) to negate the result of the condition, no matter what kind of CondPattern is used.
and ^ (regex)
Regex vocabulary
^ Called an anchor, matches the beginning of the string
/|$ is also a regular expression
/ matches a slash and has no special meaning
| is a special symbol and means or
$ is also an anchor and means end of string
So /|$ translates to: match a slash or match end of string.
Well I would suggest understanding what you are using before using it. The condition you have is opposite of what you are wanting. You should be researching regex characters rather than basic .htaccess help because then that will tell you what those mean.
The ! means not as in if not this page, then. So you need to remove that since you want to match on that page. ^ means start of the line basically. $ means the end and nothing else after that. Don't really need that in this case.
What you probably want is simply.
RewriteCond %{REQUEST_URI} ^/thediretory/? [NC]
This is probably more of a check than a question, but is the following correct:
RewriteRule ^_images/(.*)_(.*)_(.*)$ /_images/$2_$3 [L]
Basically I want to automatically make webpages use /_images/picture-name_ABC123_600.jpg (which doesn't really exist) but actually use the image /_images/ABC123_600.jpg that does exist.
I gave it a check locally in XAMPP, and worked, however I though that I'd better try adding more underscores eg. /_images/picture-name_A_B_C_1_2_3_600.jpg which I was expecting to work, yet the image still appeared - so is this normal behavour?
I'm also not too sure if I can use an underscore between the two dollar signs above...
Any help would be great!
This will not work as the (.*) is greedy and matches as many characters as it can, even underscores. Something like this (not allowing underscore between the separators):
RewriteRule ^_images/([^_]*)_([^_]*)_([^_]*)$ /_images/$2_$3 [L]
or this (ungreedy matching):
RewriteRule ^_images/(.*?)_(.*?)_(.*?)$ /_images/$2_$3 [L]
or a combination of both should work. (Depending of what chars are allowed within each block)
im into SEO and friendly URL's and im trying to create a rule in my htacess file and i need help...
Basically, i have a list of alphabet letters. If the users selects one letter, the db will show all the lyrics that starts with that letter...
so if i click C, there will be a list of lyrics and the the first is 'Car and blues'
So, from this
htpp://www.website.com/lyrics.php?letter=C
i want to do this:
http://www.website.com/lyrics/C/
so far, this is what i have
RewriteRule ^lyrics/$ /lyrics.php?letter=$1 [L]
the rule should be smart enough to pick everything that comes after 'lyrics', in between the 2 slashes, and not what comes after...
Thanks
the rule should be smart enough to pick everything that comes after 'lyrics', in between the 2 slashes, and not what comes after...
Your rule as it stands is looking for exactly lyrics/ with no possibility of anything before or after it (as defined by the ^ and $).
Assuming you're using letters A-Z in only capitals, you can use this:
RewriteRule ^lyrics/([A-Z])/?$ /lyrics.php?letter=$1 [L]
This will look for a single capital letter after the lyrics/ and send that value to the rewrite URL and also match both cases of having a trailing / or not.
the rule should be smart enough to pick everything that comes after
'lyrics', in between the 2 slashes, and not what comes after...
I'd suggest you look into using regular expressions to format your url. See this link