octavian
octavian

Reputation: 321

RewriteRule for htaccess using regex

I need to redirect /start to /start/index.html and everything else (/anything) to /index.html

What is the correct regex rewriterule for this?

Thanks

Upvotes: 1

Views: 84

Answers (2)

Felipe Alameda A
Felipe Alameda A

Reputation: 11799

I need to redirect /start to /start/index.html and everything else (/anything) to /index.html. What is the correct regex rewriterule for this?

You may try this in one .htaccess file at root directory:

Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI}  !index\.html      [NC]
RewriteRule ^start/(.*)  /start/index.html?$1 [L,NC,QSA]

In the rule, anything is passed as a query to index.php and the QSA flag adds the incoming one when present.

To pass anything as a path segment, replace ?$1 with /$1 and remove the QSA flag. It's useless, as any incoming query will be appended automatically.

For permanent redirection, replace [L,NC,QSA] with [R=301,L,NC,QSA]

Upvotes: 1

Rewrite condition is needed:

RewriteRule ^start/?$ /start/index.html [R]

RewriteCond %{REQUEST_URI} !^/start/?$
RewriteRule ^([a-zA-Z0-9_-]+)/?$ /index.html [R]

Please considered this link about the [R] flag: https://stackoverflow.com/a/15999177/2007055

Upvotes: 1

Related Questions