Clean url with rewrite rule does not work - regex

I read some tutorials about getting clean urls. I tried some of the codes they gave in the tutorials but I can't get a simple example to run...
I would like to change: http://domain.com/brigandze/mannen/index.php?p=0
To: http://domain.com/brigandze/mannen/0/
I used this code and put it in the directory domain.com/brigandze/mannen/
I have this code in my htaccesfile:
RewriteEngine on
RewriteRule ^([0-9]+)/?$ index.php?p=$1 [NC,L]

You need to either set your RewriteBase correctly or use the full path in your RewriteRule (see here for RewriteBase documentation).
RewriteBase /brigandze/mannen/
RewriteEngine on
RewriteRule ^([0-9]+)/?$ index.php?p=$1 [NC,L]
or
RewriteEngine on
RewriteRule ^brigandze/mannen/([0-9]+)/?$ brigandze/mannen/index.php?p=$1 [NC,L]
The problem is that the web server looks at URLs starting from the DocumentRoot unless you tell it otherwise. In your case, that means that your rule is trying to rewrite
http://domain.com/0/
By adding the RewriteBase, it will look at the correct place.

Related

Regex: "Mod Rewrite" everything from a "wildcard" to an address, without changing the address?

I really didn't know how to write the title. I changed it several times before I posted. But feel free to change it to the most appropriate question.
I also can't believe I couldn't find an answer already to this pretty basic thing I wanna to. I searched both here and on Google but couldn't find anything that answered this.
So I have this default WordPress .htaccess code:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
But what I would like to add, is the possibility of having all paths beginning with /cv/ to show the page for /cv/. So like a wildcard after, like /cv/*.
I tried with several versions of this:
RewriteRule /cv/.+ /cv/ - [L]
But none worked. Most things I tried redirected me to the "Couldn't find the page" page. But some just redirected back to /cv/. But I want the whatevers'-after-/cv/ should stay there. So if the address is for example /cv/hello, it should still be /cv/hello in the address but the page showing should be /cv/.
Don't think it should be so difficult. What have I missed?
ok, I set up a test now and got the following commands to work for domain.com/cv/hello
to redirect to domain.com/cv but keep the URL
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} \/cv\/(.+)$ [NC]
# make sure to exit here, if there already was a redirect (to prevent endless redirecting)
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^.*$ /cv/%1 [NC,P,R=301,L]
The "magic" is to FollowSymlinks, to use P that tells apache to proxy-pass the redirect, so that the URL remains the same, and to check if there already has been a redirect to the current URL in order to avoid endless redirecting
I solved it temporary (not a nice solution but..) by adding a rewrite rule in my functions file. So that everything from cv/* points to a specific page. In this case page with ID 8472.
/**
* Add Rewrite Rule
*/
function custom_rewrite_basic()
{
add_rewrite_rule('^cv/(.+)/?', 'index.php?page_id=8472', 'top');
remove_action('generate_after_header', 'generate_featured_page_header');
}
add_action('init', 'custom_rewrite_basic');
So, this is just the solution for WordPress. But I don't know. Maybe the other answers on this page would have worked if it wasn't WordPress.

mod_rewrite all /something to index.html

I'm trying to rewrite all urls of the following pattern:
http://example.com/csfdg/anything
into http://example.com/csfdg/index.html
my .htaccess file at the root level contains this:
Options +FollowSymLinks
RewriteEngine On
RewriteRule \/csfdg\/.* /csfdg/index.html [L]
The checker at http://htaccess.madewithlove.be/ tells me that it would rewrite my URLs the way I want, but when I go to http://example.com/csfdg/anything I just get a 404. It's very hard to tell what's going on, but I know that the RewriteEngine is working because if I mess with it enough I can get 500 errors to happen :)
Any thoughts? Thanks
You can use this :
RewriteEngine on
RewriteCond %{REQUEST_URI} !index\.html$
RewriteRule ^([^/]+)/.+$ /$1/index.html [L]
RewriteConditon is important here to avoid rewriting /foo/index.html to itself.

Redirect from domainA.example.com and domainB.example.com to domainC.example.com/test

I have 2 domains:
domainA.example.com,
domainB.example.com
And they need to be redirected to: domainC.example.com/test
The CNAME is already changed so if i go to domainA.example.com i go to domainC.example.com but it needs to point to the subfolder test.
i've read that you can do that with .htaccess but in which .htaccess file do i have to put the RewriteCond? is it in the domainC.example.com directory or in the subfolder?
I've never used the RewriteCond so if somebody could show me something that should do the trick it would really come in handy.
thx.
Yes you can do that via mod_rewrite rules in .htaccess.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(domainA| domainB)\.example\.com$ [NC]
RewriteRule ^ http://domainC.example.com/test%{REQUEST_URI} [R=301,NE,L]

Redirecting to other domain with parameters

This is the initial url:
http://example.net/any/number/folders/param1/param2/value2/value1
and this is the url we need to be mapped to:
http://example.com/any/number/folders/param1.php?param1=value1&param2=value2
The script param1.php is under directory param1 and both exist.
I have these rules but don't work. Usually get a 404 error.
RewriteEngine On
RewriteBase /param1
RewriteCond %{REQUEST_URI} /param1
RewriteRule ^(.*)(param1)/(.*)/(.*)/(.*)$ http://example.com/$1/$2.php?$2=$5&$2=$3 [L,R=301,QSA]
I have been trying to make it work for hours and also searched for something similar. At this point I am not even sure if it is possible.
Any suggestion or solution will be really appreciated. Thank you.
I just made some minor adjustments, you may try this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} /param1
RewriteRule ^(.*)(param1)/([^/]*)/([^/]*)/([^/]*)$ http://example.com/$1$2.php?$2=$5&$3=$4 [L,R=301]

How can I use htaccess to solve domain.com/domain.com/URI from loading?

I've got a problem with Codeigniter where it's processing
domain.com/domain.com/URI
Without generating a 404 error. Basically it's just loading domain.com/URI without chaning the url. So, I've got a potential duplicate content problem on my hands.
Rather than trying to hack the core, I'd like to change the htaccess to redirect
domain.com/http://domain.com/URI
to simply
domain.com/URI
I've tried googling solutions but none of them seem to be triggering the rewrite condition. Any ideas?
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+http://[^/]+(/[^\s]*) [NC]
RewriteRule ^ %1 [R,L]