Rewrite Portion of URL Only - regex

I am trying to rewrite a URL so that this-area-profile- will change to area-profile-, with everything after "profile-" matching what was in the original URL:
http://example.com/a-directory/this-area-profile-everything-after-profile-
http://example.com/a-directory/area-profile-everything-after-profile-
The below is my latest of numerous attempts, based on what I have read here and on other websites:
RewriteRule ^a-directory/area-profile-(.+)$ /a-directory/this-area-profile-$1 [R=301,L]
The other answers talk about changing or removing a directory, but I want to rename a portion of the URL.

I know my english is bad, but I understand the opposite from your explanation:
RewriteRule ^a-directory/this-area-profile-(.+)$ /a-directory/area-profile-$1 [R=301,L]

Related

My apache rewrite only works for the first folder level

We have a website where we show clients creative work we have produced for them. We upload raw assets to a path like this:
x.com/clients/clientName/campaignName/size/
I have a PHP script which adds our branding, contact information and other information and pulls in the raw creative (usually a swf object). It is in this directory x.com/clients/index.php and it accepts a query string parameter ?path so it knows where to look for the creative.
I am trying to do an apache rewrite in .htaccess so that our designers can upload directly to the known folder structure but so that when you go to x.com/clients/clientName/campaignName/size/ it should rewrite to x.com/clients/index.php?path=clientName/campaignName/size/
I am currently using the following rewrite rule, which works for the first folder level e.g. x.com/clients/clientName/ does successfully rewrite, but any subsequent folders do not.
RewriteRule ^clients/([^/\.]+)/?$ /clients/index.php?path=$1 [L]
My RegEx's are terrible, so I'm stuck on what to do. Any help appreciated, thank you kindly.
Your regex is only matching urls like clients/xxxxxx/ because your pattern [^/\.]+ means one or many characters except "/" or "."
With your rule, it can't work for other subdirectories.
You can change your rule by this one
RewriteRule ^clients/(.+)$ /clients/index.php?path=$1 [L]
To avoid internal server error (code 500 which means an infinite loop in this case), you can do it this way
RewriteRule ^clients/index\.php$ - [L]
RewriteRule ^clients/(.+)$ /clients/index.php?path=$1 [L]
Is there a special reason you want to use regex? In my opinion you can just catch everything coming after /clients:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^(.*/)?index\.php$ [NC]
RewriteRule ^clients/(.*)$ /clients/index.php?path=$1 [L]
The second line is to prevents redirect loops, because the index.php is also in the folder /clients and this would cause never ending redirects.

.htaccess | Rewrites with pages

So I'm adjusting the URLs on a site I'm working on and I'm having some trouble with a couple of variables being passed in the URL.
mylighting.com/bath-fixture-c-13.html?osCsid=u2qj8o9rvjn0p5pa7p8npuhs54
RewriteRule ^bath-fixture bath-fixture-c-13.html?id=$1
So this Rewrite works perfect as the page that comes up is mylighting.com/bath-fixture
Now unfortunately on that page there are several other items to view and I'm having some trouble with the page variable. I think I have the code correct but every time I try to go to the correct page it doesn't seem to work.
http://mylighting.com/bath-fixture-c-13.html?page=2&id=u0hnumfus6gjhjc45av36663m3
RewriteRule ^bath-fixture/([a-zA-Z0-9]+)$ bath-fixture-c-13.html?page=$1&id=$2
So I thought I had this correct but apparently not. I would like the output to be
mylighting.com/bath-fixture/2 for the second page.
Unfortunately with that code in the .htaccess, every time I input that URL it takes me to the first page of the category and not the second like it should.
It appears that you have misunderstood the format of the RewriteRule. The first one is working by accident.
RewriteCond %{QUERY_STRING} page=([0-9]+)
RewriteRule ^bath-fixture-c-13.html bath-fixture/%1 [R=301,QSA,L]
RewriteRule ^bath-fixture-c-13.html bath-fixture/ [R=301,QSA,L]
The first argument to RewriteRule is the regex to match against the requested URL. The second argument is the URL to send as a Redirect to the user, so they end up at your desired URL instead. Because you want to parse the QUERY_STRING I believe you need to use a RewriteCond. If you are doing this for SEO purposes do not forget to add the [R=301].
http://httpd.apache.org/docs/current/mod/mod_rewrite.html
If you are trying to go the other direction then you need the following.
RewriteRule ^bath-fixture/$ bath-fixture-c-13.html [QSA,L]
RewriteRule ^bath-fixture/([0-9]+) bath-fixture-c-13.html?page=$1 [QSA,L]
If you can clarify your desired inputs and outputs you will get correct answers much more quickly.

htaccess redirect 301 with forward slash in parameters won't work

I have done some research so far, but could not find the solution. I have a client who has this old file structure on the URL:
/index.php?process=views/article.php&articleId=44102
which should redirect to this URL:
/news/title-of-article/
I found that I may need this in my htacces file:
RewriteCond %{QUERY_STRING} &articleId=44102 [NC]
RewriteRule .* /news/title-of-article/ [R=301,L]
and when I test it with the full URL, it won't redirect. But when I leave out the forward slash like this:
/index.php?process=viewsarticle.php&articleId=44102
it redirects fine. So I assume the forward slash is the problem here.
I'm not a specialist in setting up .htaccess files and this drives me crazy, because it seems that I have the right solution in plain sight, but don't know why the forward slash is preventing the redirect.
If you can't answer with the final solution, I'd appreciate to have some kind of tool (website, software, etc) where I would be able to test these rewrite conditions in detail. Maybe there is something like the regex-testers that are available for regular expressions, but just for RewriteCond and RewriteRules that I can use to figure it out myself.
Thanks in advance
Marian
Problem is that you're not stripping out QUERY_STRING In rewritten URI and because of that resulting URL also contains QUERY_STRING as process=views/article.php&articleId=44102 and it matches rule again causing redirection loop.
Change your rule to this:
RewriteCond %{QUERY_STRING} &articleId=44102 [NC]
RewriteRule .* /news/title-of-article/? [R=301,L]
? in the end will strip out existing query string.

Proper regex for .htaccess redirect and prevention of hotlinks

I posted this on Reddit (r/learnprogramming) and someone there PM'd me and told me to come here, so here I am!
I have been trying to learn regex's and I suck at them still. I seriously have difficulty grasping the pattern matching. I am solid in other OOP languages so I figured I would learn regex and it just evades me.
I have downloaded EditPad Pro so I can practice as http://www.regular-expressions.info/tutorial.html suggests. I can get expressions to match bulk text, but I am trying to parse URL's and I keep missing.
Here is what I am trying to do. I am writing my own permalink .htaccess file as a proof of concept study, so I can hopefully use this is in future sites.
I need to return the following dynamic content from a URL:
I need everything other than http:// www.domain.com/ or http:// domain.com/ or domain.com/:
(I am adding a space after http:// because of the limits on new accounts)
http:// www.domain.com/asdjh324hj.jpg
http:// www.domain.com/asa45s.png
http:// www.domain.com/aser24hj.gif/
http:// www.domain.com/wer234dsfa/
http:// www.domain.com/k3kjk4
http:// www.domain.com/k3kasd4/
The matched part will then be appended to:
http:// www.domain.com/some_dir/som_subdir/some_file.php?querystring=$1
But, I don't want any of these urls in the results:
http:// www.domain.com/some_dir/some_file.php
http:// www.domain.com/some_dir/some_subdir/some_file.html
And I need to prevent hotlinking to images in the image_dir:
http:// www.domain.com/image_dir/some_dir/some_subdir/some_image.jpg (or png,gif,etc)
Hotlinked images would be redirected to a page with the passed image as a querystring.
So what RewriteRule regex would I setup to grab this? I understand RewriteRules and the flags, putting matched results into variables, etc, I just can't figure out what regex I should write to grab the actual result.
If this is too complex for RewriteRules, then please let me know as I am struggling here.
Usually I do these in PHP and would start with:
.com/[a-zA-Z0-9-_.]+
([^/]+)/?$
Then do good 'ol substrings and checks. It's hacking it to death and I should be doing better!
I am currently going through the regular-expressions.info tutorials and am making progress, but I keep grabbing the wrong things too.
Thanks for any help you can send my way!
Update: I was able to resolve everything with a ton of help and discussed more here: Mod_Rewrite conditions help for hotlinking but allow local requests
I need everything other than http:// www.domain.com/ or http:// domain.com/ or domain.com/:
RewriteCond %{REQUEST_URI} !^/$
But, I don't want any of these urls in the results:
RewriteCond %{REQUEST_URI} !^/some_dir/
The matched part will then be appended to:
RewriteRule ^(.*)$ /some_dir/som_subdir/some_file.php?querystring=$1 [L]
So in all it should look something like this:
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{REQUEST_URI} !^/some_dir/
RewriteRule ^(.*)$ /some_dir/som_subdir/some_file.php?querystring=$1 [L]
Which will make it so when you request something like http://www.domain.com/asa45s.html, it will get internally rewritten to some_dir/som_subdir/some_file.php?querystring=asa45s.html. As for the hotlinking bit:
RewriteCond %{REQUEST_URI} ^/image_dir/
RewriteCond %{REQUEST_URI} \.(png|gif|jpe?g|bmp|ico)$ [NC]
RewriteCond %{HTTP_REFERER} !^https?://(www\.)?domain.com/
RewriteRule ^(.*)$ /some_dir/som_subdir/some_file.php?querystring=$1 [L]
This checks that first the request is for something in the /image_dir/ directory, then that the requested resource ends with a png/gif/jpeg/bmp/ico extension, then that the HTTP referer [sic] does not start with http://www.domain.com/, https://domain.com/ or whatever combination of the 2. If all those are true, then it rewrites the request to the /some_dir/som_subdir/some_file.php file with the original URI as the querystring parameter.

.htaccess RewriteRule for multiple pages

I've done RewriteRule for index page in my website.
Here is the code below:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^([a-zA-Z0-9+\-\(\)]+)/?$ /index.php?origin=$1
RewriteRule ^([a-zA-Z0-9+\-\(\)]+)/([a-zA-Z0-9+\-]+)/?$ /index.php?origin=$1&gender=$2
RewriteRule ^([a-zA-Z0-9+\-\(\)]+)/([a-zA-Z0-9+\-]+)/([a-zA-Z0-9+\-]+)/?$ /index.php?origin=$1&gender=$2&type=$3
RewriteRule ^([a-zA-Z0-9+\-\(\)]+)/([a-zA-Z0-9+\-]+)/([a-zA-Z0-9+\-]+)/([a-zA-Z0-9+\-]+)/?$ /index.php?origin=$1&gender=$2&type=$3&page=$4
</IfModule>
and the url is looks like
http://babynames.agurchand.com/scottish/male/pythagorean
It's a single page website, so everything is working fine so far.
Then i wanted to add one more page on my website and i have added too. file name is 'namemeaning.php'.
I've added the below code in addition of the htaccess pasted, at the bottom. But It doesn't seem to be working.
RewriteRule ^/namemeaning/([a-zA-Z0-9+\-\(\)]+)/?$ /namemeaning/namemeaning.php?name=$1
I've tried with the below url, but I'm still being referred to the index page only.
http://babynames.agurchand.com/namemeaning/abasi
Can anyone give me a solution for this please!
You're being forwarded to the index page because your regular expression for the index page fits your request. You're being sent to index.php?origin=namemeaning&gender=abasi
http://babynames.agurchand.com/namemeaning/abasi
'namemeaning' fits the rule ([a-zA-Z0-9+\-\(\)]+)
'abasi' fits the rule ([a-zA-Z0-9+\-]+)
A simple fix would be adding the line
RewriteRule ^/namemeaning/([a-zA-Z0-9+\-\(\)]+)/?$ /namemeaning/namemeaning.php?name=$1
above the line
RewriteRule ^([a-zA-Z0-9+\-\(\)]+)/?$ /index.php?origin=$1
You should remember to always place the most specific RewriteRule(s) above those that are more general.
For more information about regular expressions, I can advice you this website.
The rewruite rules are checked sequentially from top to bottom, and all rules matched are applied in that order. Double check that none of your rules match before the desired one.
Good Tool for that purpose is http://www.regextester.com/