I'm trying to redirect URLs with .htaccess in order to be able to make this:
request: http://www.example.org/business/businesswhatever/
redirect: http://www.example.org/businessgrowing/businesswhatever/
The more close I am of reaching the solution is to apply this code:
RewriteRule ^(.*)example\.org/businessgrow/(.*)$ $example.org/businessgrowing/$2 [R=301,L]
but it does not work at all. Validator doesn't check a thing, the only way to validate is if I omit http:// part of the URL.
I need to use hostname as part of the regex because some changes were made and now businessgrow is inside this path: www.example.org/corp/businessgrow, so I need to take part of the hostname in the regex in order to distingish between example.org/businessgrow (it must be a 404) and example.org/corp/businessgrow (200 OK)
I'm using this checker http://htaccess.madewithlove.be/ in order to test before deploying the solution. I tried to remove a slash from http:// (because I was unable to think anything else, I already tried anything) and it worked, but validator (I mean, it doesn't skip validation) outputs 3 slashes like this http:///www.example.org/en/businessgrow/businesswhatever/
If I input the URL without http:// code above works perfectly, but Apache doesn't recognise it and website is unable to show a single page.
I'm using Debian wheezy server and I'm using Wordpress, i took care of write this line just below REwriteBase line, so my .htacces is like this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)example\.org/businessgrow/(.*)$ $1example.org/businessgrowing/$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I would appreciate if anybody gives me a clue in order to solve this, I think there's some mistake with validation process but I can't find it.
Thanks so much for your time!
btw, sorry about my english..
You don't want the hostname as part of the regex, only the request URI path is used to match. Try:
RewriteCond %{HTTP_HOST} example\.org$ [NC]
RewriteRule ^businessgrow/(.*)$ /businessgrowing/$1 [R=301,L]
Related
I try to redirect to another domain and changing the URL structure (not keeping URI part) based on 2 conditions:
The original URL has "lang" parameter.
The original URI begins with "page".
It's Apache 2.2
This is working when only have the lang paramenter in the orginal URL:
RewriteCond %{QUERY_STRING} lang=en$
RewriteRule ^.*$ https://www.destination.com/en/? [R=301,L]
For example it redirects ok for:
http://www.origin.com/?lang=en
to:
http://www.destination.com/en/
But I also need to redirect something like:
http://www.origin.com/page/5/?lang=en
to:
http://www.destination.com/en/
I'm trying things like this, but doesn't work, I think there's something wrong in the RewriteRule pattern:
RewriteCond %{QUERY_STRING} ^lang=en$
RewriteRule ^/page/.*/?$ https://www.destination.com/en/? [R=301,L]
Welcome!
You may not want to do have a ? at the end of your RewriteRule. However, if you wish to have you can do so. Maybe, you want to have a RewriteRule similar to:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} lang=en$
RewriteRule (.*)(\?lang=en) https://www.destination.com/en/ [R=301,L]
</IfModule>
You might want to restart your apache, with a command similar to:
sudo apachectl restart
You might clear your browser cache every time that you make a change on .htaccess file
Graph
This graph shows how your expression works, if you wish to know:
This tool can help you to simply design any rule that you like to have.
I have assumed that you want to redirect all your ?lang=en containing ULRs to a single URL. I was not sure about that. If that's not the case, you may not use my expression.
I need some help with rewriting an url to lowercase.
So, i have an apache server which is a proxy for a couple of applications (some on IIS and some on JBoss). The JBoss app has app context.
The problem is if i try to access an app over https://www.domain.com/App i get a 404.
If i try https://www.domain.com/app all is fine.
If i do a catch all RewriteRule to lowercase all uppercase then all my links and scripts get lowercase which is not really working.
In that case, a https://www.domain.com/app/SomeFolder/SomeScript.js link, would look like this https://www.domain.com/app/somefolder/somescript.js.
What i want is, if a https://www.domain.com/ApP/SomeFolder/SomeScript.js url is entered, to change it to https://www.domain.com/app/SomeFolder/SomeScript.js
My Apache config to catch uppercase letters and lowercase them is:
RewriteMap lc int:tolower
RewriteCond %{REQUEST_URI} ^(/[A-Z]+/?)
RewriteRule ^([^/]*/?)(.*)$ ${lc:$1}$2 [R=301,L]
But this doesn't work and i can't seem to find the correct regex for the cond to catch only the app context and not the whole REQUEST_URI as my log says not matched.
Hope all this made sense. If more info is needed i will provide it.
Ty again.
EDIT:
The (?<=.com/)[^/\s]+ regex catches what i need. The question now will Apache work with this regex.
SO, the new ruleset should look like this(?):
RewriteMap lc int:tolower
RewriteRule ((?<=\.com/))([^/\s]+) ${lc:$1}$2 [R=301,L]
EDIT2:
Since i got no more feedback on this, i went and tried some other methods, and i ended up with this:
RewriteCond %{REQUEST_URI} !^/[A-Za-z]+/
RewriteCond %{REQUEST_URI} !^/[^a-z]+/
RewriteRule ^(.*)$ $1/ [R=301]
RewriteCond %{REQUEST_URI} ^/[A-Za-z]+
RewriteCond %{REQUEST_URI} ^/[^a-z]+
RewriteRule ^(.*) ${lc:$1} [R=301]
RewriteCond %{QUERY_STRING} "ReturnUrl="
RewriteRule ^(.*) $1? [R=301]
This does all i want except lowercase an uppercase URI when it doesn't start with an uppercase. So a /APp uri will get rewritten to /app but /aPp won't be. I'll just leave this here, hopefully i'll get some feedback on this.
One problem now is that sometimes the loading takes >15 seconds. And i can only relate this to the rewrite having, in the worst case, 3 301 redirects...
find the correct regex for the cond to catch only the app context
this regex will grab everything after "app/":
(?<=app/)[\S]+
now each whole match can simply be fed to a "toLowerCase()" function equivalent.
regex demo
I'm in desperate need of a quick tip.
Trying to use htaccess to change this not so lovely url
http://localhost/test/index.php?page=Article&articleID=61
to
http://localhost/test/article/2015-09-21-this-is-the-headline
From what I've gathered I need to send the last part to a php script which can then get the matching id from the database. Knowing that I should be able to send the user to the original url up top.
RewriteRule ^(.*)\/article\/(.*)$ redirect/article.php [L]
# RewriteRule ^(.*)$ index.php
As of right now I'm not passing the information to the script yet. redirect/article.php only contains a print statement to let me know once I get that far.
However, despite my brain and every regex debugger saying otherwise, it won't match the url provided in the second code box. All I'm getting is the good old 404. If I activate the second rule it is applied to my url, telling me that the first one is simply being skipped.
What am I missing?
.htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
# rename individual pages
RewriteRule ^(.*)\/article\/(.*)$ redirect/article.php [L]
# RewriteRule ^(.*)$ index.php
# resize images
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.)*\/([0-9]+)\-(([0-9]|[a-z])+)\.(prev)$ filePreview.php?id=$2&size=$3 [L]
php_value upload_max_filesize 20M
php_value post_max_size 21M
</IfModule>
The location of a .htaccess file informs how you must list paths for mod_rewrite. Inside .htaccess, paths for RewriteRule are not received with a leading /. Since yours was residing in /test, the (.*) at the start of your rule wasn't matching anything and harmless. Since that was followed by /, the article/ path was expecting a / it would never receive. The simplest fix is to change this rule to match article at the start via:
RewriteRule ^article/(.*) redirect/article.php [L]
Assuming you'll use that as a lookup in the PHP script, add a parameter to use the $1 captured pattern like:
RewriteRule ^article/(.*) redirect/article.php?article=$1 [L]
It's my first request here and hopefully I won't upset anyone.
Here's my story:
I have looked all over this place for a solution and wasn't able to find one. Here's hoping that someone can give some input.
I've basically managed to use Apache's mod_rewrite to create SEO-Friendly urls for my website.
E.g. Old path www.hostname/index1.php?session=user is now rewritten as www.hostname/user, which results into a SEO Friendly URL address.
However, the old path is still valid. I need to somehow redirect all the incoming old index1.php requests to the newer URLs, the SEO-Friendly ones, for the search engines to transfer the link popularities to the new ones. I believe I may have an infinite-loop redirect and that's why it's not working.
My code so far:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
## Redirect still valid old links to SEO friendly ones
RewriteRule ^/?index1\.php\?session=user$ /user [R=301]
## Catch the above and rewrite the URL
RewriteRule ^/?user/?$ /index1.php?session=user [QSA,L]
The above rules never get hit when the htaccess file is parsed.
It hit me that I might be doing some sort of redirect loop here so I thought about renaming the index1.php file to index2.php and create something like:
## Redirect still valid old links to SEO friendly ones
RewriteRule ^/?index1\.php\?session=user$ /user [R=301]
## Catch the above and rewrite the URL
RewriteRule ^/?user/?$ /index2.php?session=user [QSA,L]
However, that failed too.
What would be the best approach to this? What am I doing wrong here?
Thank you!
Update your .htaccess rules to
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
## Redirect still valid old links to SEO friendly ones
RewriteCond %{QUERY_STRING} !no-redir [NC]
RewriteCond %{QUERY_STRING} session=user [NC]
RewriteRule ^/?index1\.php$ /user? [R=301,NC,L]
## Catch the above and rewrite the URL
RewriteRule ^/?user/?$ /index1.php?session=user&no-redir [QSA,NC,L]
You can't match against the query string (everything after the ?) in a rewrite rule, so you can't match against the session= part. You also can't simply match against the %{QUERY_STRING} var because that gets populated by you other rule when it rewrites the SEO friendly URL to the one with the query string. So you need to match against the actual request:
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /index1\.php\?session=([^&]+)&?([^\ ]*)
RewriteRule ^ /%2?%3 [L,R=301]
I have made an .htaccess to my root directory for creating a subdomain level, assume it is sub.domain.ex that redirect to domain.ex/deb/
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} sub.domain.ex
RewriteCond %{REQUEST_URI} !deb/
RewriteRule ^(.*)$ /deb/$1 [L]
and this works well.
Now, I go to /deb/ and create another .htaccess with the following
RewriteEngine on
RewriteRule ^([^/]+)/ /deb.php?app=$1 [NC]
the deb.php is a file that prints the argument "app".
It works but only if i call http://sub.domain.ex/something/ (note the slash at the end). It only works with a final slash, if I remove it, it doesn't and I want it to work without final slash.
So I change the rule into ^([^/]+) but now I have 500 Apache internal error.
The regex coaches are by my side with the selection, maybe I'm missing something.
Thanks
UPDATE
I'm runinng mad. Mybe is wrong to put one .htaccess in the root for creating se 3th sublevel doman and the .htacces in the other directory? Because I'm ttrying some trick.
I use this rule
RewriteEngine on
RewriteRule ^([^/]+)/ deb.php?app=$1 [NC]
in the .htacces of the /deb directory and made a print_r($_GET); and called index.php. So the redirectory doesn't work at all if it forward me on the index.php of the cydia sublvel and doesn't take the /deb/deb.php!!!
Recap.
my dir structure is this:
/htdocs/ -> the root of the main domain level like www.example.com
---index.php -> home file of www.example.com
/htdocs/deb -> the root directory of the 3th sublevel domain (subdomain.example.com ->
---index.php
---deb.php
So the .htaccess for the 3th level domain is the in /htdocs./htaccess and described as before.
The other .htaccess for "beautify" the link is in the /htdocs/deb/.htaccess. I want that when you go to subdomain.domain.com/someText it transform to deb.php?app=someText
Now i tryed go to subdomain.domain.com/deb.php....WTF!? the deb.php is in /htdocs/deb/deb.php
Home is clear
This works fine for me, at least, assuming I understood everything you wanted.
In /htdocs/.htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} =sub.domain.ex
RewriteCond %{REQUEST_URI} !^deb/
RewriteRule ^(.*)$ /deb/$1
In /htdocs/deb/.htaccess:
RewriteEngine On
RewriteBase /deb/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ deb.php?app=$1
RewriteRule !^deb.php index.php
Edit: I updated the second file's contents to reflect your additional requests. You should remove the /? if you don't want them to be able to go to sub.domain.ex/something/ -> deb.php?app=something.
I could be wrong, but in the second .htaccess you should check that the url wasn't yet rewrited with "/deb.php"
So something like
RewriteCond %{REQUEST_URI} !deb\.php
RewriteRule ^([^/]+) /deb.php?app=$1 [NC]
You rewrite rule should be
RewriteRule ^(.*)$ deb.php?app=$1 [L,QSA]
That's a common pattern used in CMSs like Drupal
I think there is some kind of rescrctions to my host. It's impossible. I tried all kind of goo (for a regexcoach) for matching the correct group and there is nothing else the 500 error apache.
The only way to get this to work is use tu parameters
with this ^app/([^/]+) /deb.php?app=$1 and calling with sub.domain.com/app/nameTest and this is working with or without the end backslash.
Of you get some advice for getting rid of this, please let me know.
bye