Apache2 ModRewrite Dynamic Url - regex

I am trying to use htaccess to rewrite my url variables.
Example url http://example.com/assets/asset.php?id=9e233a25-994e-4262-8bfb-e477de1474ae
what I need is
http://example.com/assets/9e233a25-994e-4262-8bfb-e477de1474ae
My htaccess file looks like this:
RewriteEngine On
RewriteBase /
RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /asset\.php\?id=(.*)\ HTTP
RewriteRule ^ /%1\? [R,L]
RewriteRule ^(.*)$ /asset.php?id=$1 [L]
What should I put into my htaccess file as this is not working?

Place this code in your DocumentRoot/.htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+assets/asset\.php\?id=([^\s&]+) [NC]
RewriteRule ^ /assets/%1? [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^assets/([^/]+)/?$ assets/asset.php?id=$1 [L,QSA,NC]

Related

url rewriting not working as wanted

I am trying to achieve a clean url that looks like http://localhost/slugs/this-is-my-first-slug-post
but instead I have http://localhost/slugs/news.php?newsheadline=this-is-my-first-slug-post
I have an htaccess file with this code but it doesn't seem to work.
RewriteEngine On
RewriteRule ^[A-Za-z-]+/([A-Za-z0-9-]+)/?$ news.php?newsheadline=$1 [NC,L]
You can use this rule:
RewriteEngine On
RewriteBase /slugs/
RewriteCond %{THE_REQUEST} /news\.php\?newsheadline=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ news.php?newsheadline=$1 [QSA,L]

rewrite rule for subfolder index

i have this url
http://myurl.com/shiftturn/index.php?m=value
and i want:
http://myurl.com/shiftturn/value
this is my .htaccess
RewriteEngine On
RewriteRule /shiftturn/([^/]*)$ /index.php?m=$1 [L]
but doesn't work.
how can I do to get the kind of url I need, with a correct htaccess expression?
Place this rule in /shiftturn/.htaccess:
RewriteEngine On
RewriteBase /shiftturn/
RewriteCond %{THE_REQUEST} /index\.php\?m=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?m=$1 [L,QSA]

.htaccess internal redirect not working

I tried doing some .htaccess redirects for internal pages but they are not working for me. This is my .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
RewriteRule articles articles/how-to-play-piano [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule . index.php [L]
</IfModule>
I also tried this outside <IfModule> but not working:
Redirect 301 http://www.domain.com/articles http://www.domain.com/articles/how-to-play-piano
and
Redirect 301 /articles http://www.domain.com/articles/how-to-play-piano
Your regex is wrong for articles rule and will cause infinite looping. To fix that you need to use anchors ^ and $:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
RewriteRule ^articles/?$ articles/how-to-play-piano [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule . index.php [L]

.htaccess force https but exclude a subdomain

How would I exclude a subdomain from getting forced to https and redirected to main url? I'm using the below in my .htaccess file but need a little help as my subdomain (stage.mydomain.com) gets redirected / can't access.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I've tried a few options but no luck - Any ideas? Thanks!
Have your first rule like this:
RewriteCond %{HTTP_HOST} !^stage\.mydomain\.com$ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

I need a htaccess RewriteRule for a simple pattern

I want something like the following:
http://fref.co/anystring
to be rewritten as:
http://fref.co/?q=anystring
This is how far I've gone:
RewriteEngine On
RewriteRule ^(.+)/?$ index.php?q=$1 [NC,L]
This should work for your needs:
Options +FollowSymLinks -MultiViews
RewriteEngine On
# Internally redirect http://fref.co/anystring to http://fref.co/?q=anystring
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ /index.php?q=$1 [L]
# Redirect http://fref.co/?q=anystring to http://fref.co/anystring
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(/index\.php|/)\?q=([^\s]+) [NC]
RewriteRule ^ /%2? [R=302,L]