.htaccess rewrite rule not working for index.php - .htaccess

I am trying to use pretty urls so https://example.com/event/1/dog-singing will load https://example.com/events?action=show&id=1
I added the following code to my .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^event/([^/]*)/?(.*)$ events?action=show&id=$1 [L,NC]
I get a 404 page.
I tried using
RewriteRule ^event/([^/]*)/?(.*)$ index\.php/events?action=show&id=$1 [L,NC]
Same 404 page.
The below rewrite works. But I don't want to load the script from the subdirectory.
RewriteRule ^event/([^/]*)/?(.*)$ wp-content/plugins/testplugin/testplugin\.php?action=show&id=$1 [L,NC]
The one below also works. But it changes the pretty url in the address bar.
RewriteRule ^event/([^/]*)/?(.*)$ https://example.com/events?action=show&id=$1 [L,NC]
EDIT
Here is the full .htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^event/([^/]*)/[^/]*/?$ index.php?action=show&id=$1 [QSA,L,NC]
RewriteRule ^register/([^/]*)/?(.*)$ wp-content/plugins/testplugin/testplugin\.php?action=register&event=$1&name=$2 [L]
# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Related

How to make .htaccess redirect to subdomain with parameters

How do I make a redirect from all pages following /react-api, such as:
www.example.com/react-api/workout?id=123
www.example.com/react-api/another-site/?id=321
to:
app.example.com/react-api
and I wan't to keep the folder and parameters.
This is what I have tried:
# 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]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule ^react-api/(.*) app.example.com/$1 [R=301,L]
</IfModule>
# END WordPress
But it doesn't work.
Please help
You need to use http:// in your target URL and also change order of your rules and keep redirect rule at the top:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^react-api(/.*)?$ http://app.example.com%{REQUEST_URI} [R=301,NE,NC,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Remove Trailing Slash From URLs

I'm trying to remove the trailing slash from our URLs. I adjusted our .htaccess file and added this code:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
Unfortunately this didn't work as the pages (except for the homepage) suddenly show too many redirects and I get a message from the system saying "The page isn’t redirecting properly".
I was thinking maybe I made an error in the way I inserted the code. Originally, our .htaccess file has this:
# 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
# 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
After adjusting it to insert the code, it looked like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# BEGIN WordPress
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
And that resulted in an error. Can anybody point out where I went wrong? Thanks.
Turns out it's a Wordpress issue. It's something you can adjust by going to your Wordpress site's Permalink Settings. From here you can choose your preferred URL format. "With-trailing-slash" is the default format so you definitely need to make an adjustment if you want the typical "without-trailing-slash" format.

URL rewrite for part of domain name

I feel this should be an easy fix but I'm struggling to get it done right.
I have the URL:
http://www.testing.com/toursgbr/my-post
http://www.testing.com/toursgbr/my-post-2
I need to rewrite the URL to:
http://www.testing.com/tours/gbr/my-post
http://www.testing.com/tours/gbr/my-post-2
I got as far as the following:
RewriteRule ^toursgbr/(.*) /tours\/gbr/$1 [L]
This is what's currently in the htaccess file:
RewriteEngine on
ErrorDocument 404 http://www.ausweb.com.au/web-hosting
AddHandler server-parsed html
# 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
RewriteCond %{HTTP_HOST} ^seabreezepark\.com\.au$ [OR]
RewriteCond %{HTTP_HOST} ^www\.seabreezepark\.com\.au$
RewriteRule ^/?$ "http\:\/\/theseabreezepark\.com\.au\/" [R=301,L]
RewriteRule ^toursgbr/(.+)$ /tours/gbr/$1 [NC,L]
and got nowhere pretty fast. I just want to look for the word "toursgbr" and change it to "tours/gbr" in summary.
Put the Following code at root .htaccess file :
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} !\s/+tours/gbr/ [NC]
# the above line will exclude any request having tours/gbr/ from the follwoing rule.
RewriteRule ^toursgbr/(.*)$ tours/gbr/$1 [R=302,L,NE]
# the above line will change any requested url having toursgbr/ to be tours/gbr/ temporary
# and you can change it to permanent by changing [R=302,L,NE] to [R=301,L,NE]
# but check the code as it is first then change it
RewriteRule ^tours/gbr/(.*)$ toursgbr/$1 [L,NC]
# the above line will internally map any request having tours/gbr/ to its original path
Try :
RewriteRule ^toursgbr/(.+)$ /tours/gbr/$1 [NC,L]
Do not use the full url in rewrite target if you want to internally redirect /toursgbr/foo to /tours/gbr/foo without changing the url in browser.
Your corrected htaccess :
ErrorDocument 404 http://www.ausweb.com.au/web-hosting
AddHandler server-parsed html
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?seabreezepark\.com\.au$
RewriteRule ^/?$ http://theseabreezepark.com.au/ [L,R=301]
RewriteRule ^toursgbr/(.+)$ /tours/gbr/$1 [NC,L]
# BEGIN WordPress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

i want 301 redirection to multiple pages

i want to change urls of my website, but there many pages, so i cant change them manually.
so i need to change it from htaccess file.
i want to change the below url
http://demos.beusoft.com/ccjk/resource/arabic-translation-services/
with the
http://demos.beusoft.com/ccjk/arabic-translation-services/
i am using wordpress.
this is whole code ....
# BEGIN WordPress
RewriteEngine On
RewriteRule ^ccjk/resource/([^/]+)/$ demos.beusoft.com/ccjk/$1 [L,R=301]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /ccjk/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /ccjk/index.php [L]
</IfModule>
# END WordPress
please help, how to rewrite the url from the htaccess file.
Try this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /ccjk/
RewriteRule ^resource/([^/]+)/$ http://demos.beusoft.com/ccjk/$1/ [L,R=302]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /ccjk/index.php [L]
</IfModule>
# END WordPress

redirect parked domain to another domain subfolder showing correct urls

i have 2 domains:
- domain1.org
- domain2.org
i have installed wordpress in http://domain1.org/wordpress and i need that users enter with http://domain2.org
Currently my htaccess is(based on other posts readed here):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?domain2\.org$ [NC]
RewriteRule ^(.*)$ http://www.domain1.org/wordpress/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
</IfModule>
this works but always show in the links the domain1 url:
http://domain1.org/wordpress/category/furniture/
and in the browser bar i have http://www.domain2.org
Is the htaccess wrong? i have domain2 with godaddy and i'm doing a masked forward.Maybe i should park it?
thk.
# BEGIN WordPress
<IfModule mod_rewrite.c>
# tell mod_rewrite to activate and give base for relative paths
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?PARKED.com$
RewriteCond %{REQUEST_URI} !^/PARKED/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /PARKED/$1
RewriteCond %{HTTP_HOST} ^(www.)?PARKED.com$
RewriteRule ^(/)?$ PARKED/index.php [L]
# for main domain
RewriteRule ^index\.php$ - [L]
# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule . index.php [L]
</IfModule>
# END WordPress

Resources