質問編集履歴

3

コード修正

2019/03/09 02:00

投稿

chef
chef

スコア15

test CHANGED
File without changes
test CHANGED
@@ -158,7 +158,7 @@
158
158
 
159
159
 
160
160
 
161
- $htaccess_text .= $new_text;
161
+ $htaccess_text = $new_text.PHP_EOL.$htaccess_text;
162
162
 
163
163
 
164
164
 

2

質問内容をアップデート

2019/03/09 02:00

投稿

chef
chef

スコア15

test CHANGED
File without changes
test CHANGED
@@ -96,17 +96,21 @@
96
96
 
97
97
  【ファイルA】
98
98
 
99
+ #URLはユーザーに入力してもらう
99
100
 
101
+ $new_site_url = 'https://new.com/';
100
102
 
101
- $new_url = 'https://new.com/';
103
+ #http://old.com/hello-worldの記事にアクセスした時にリダイレクトする
102
104
 
103
105
  $rewrite_cond = 'RewriteCond %{HTTP_HOST} ^old.com$'.PHP_EOL;
104
106
 
105
107
  $rewrite_cond .= 'RewriteCond %{REQUEST_URI} ^/hello-world/?$'.PHP_EOL;
106
108
 
107
- $rewrite_rule = 'RewriteRule ^([^/]+?)?$ https://new.com/$1 [R=302,L]';
109
+ $rewrite_rule = 'RewriteRule ^([^/]+?)?$ '.$new_site_url.'$1 [R=302,L]';
108
110
 
109
111
 
112
+
113
+ #ファイルB用に処理内容を出力
110
114
 
111
115
  $text = <<< EOM
112
116
 
@@ -142,7 +146,7 @@
142
146
 
143
147
  require_once(ファイルA);
144
148
 
145
- $new_text = ob_get_clean();
149
+ $new_text = ob_get_clean(); //ファイルAから処理内容を受け取って変数へ
146
150
 
147
151
 
148
152
 

1

コード追記

2019/03/09 01:55

投稿

chef
chef

スコア15

test CHANGED
File without changes
test CHANGED
@@ -29,6 +29,8 @@
29
29
  ```
30
30
 
31
31
  <IfModule mod_rewrite.c>
32
+
33
+ #元のURL:http:old.com
32
34
 
33
35
  RewriteEngine On
34
36
 
@@ -77,3 +79,101 @@
77
79
 
78
80
 
79
81
  このコードは何がいけないのでしょうか?
82
+
83
+
84
+
85
+ ## 追記
86
+
87
+
88
+
89
+ 非常にざっくりとですが、今現在このようなリダイレクト処理を自動化するプログラムを作っています。
90
+
91
+
92
+
93
+ ```php
94
+
95
+
96
+
97
+ 【ファイルA】
98
+
99
+
100
+
101
+ $new_url = 'https://new.com/';
102
+
103
+ $rewrite_cond = 'RewriteCond %{HTTP_HOST} ^old.com$'.PHP_EOL;
104
+
105
+ $rewrite_cond .= 'RewriteCond %{REQUEST_URI} ^/hello-world/?$'.PHP_EOL;
106
+
107
+ $rewrite_rule = 'RewriteRule ^([^/]+?)?$ https://new.com/$1 [R=302,L]';
108
+
109
+
110
+
111
+ $text = <<< EOM
112
+
113
+ <IfModule mod_rewrite.c>
114
+
115
+ RewriteEngine On
116
+
117
+ $rewrite_cond
118
+
119
+ $rewrite_rule
120
+
121
+ </IfModule>
122
+
123
+ EOM
124
+
125
+
126
+
127
+ echo $text;
128
+
129
+
130
+
131
+ ```
132
+
133
+ ```php
134
+
135
+
136
+
137
+ 【ファイルB】
138
+
139
+
140
+
141
+ ob_start();
142
+
143
+ require_once(ファイルA);
144
+
145
+ $new_text = ob_get_clean();
146
+
147
+
148
+
149
+ $htaccess = ABSPATH.'.htaccess';
150
+
151
+
152
+
153
+ $htaccess_text = @file_get_contents( $htaccess );
154
+
155
+
156
+
157
+ $htaccess_text .= $new_text;
158
+
159
+
160
+
161
+ file_put_contents($htaccess, $htaccess_text);
162
+
163
+
164
+
165
+ ```
166
+
167
+
168
+
169
+ なぜ$1だけピンポイントで消えるのか、どこで消えているのかが、理解できませんでした。
170
+
171
+
172
+
173
+ 詳細なコードはGithubの方で公開しているので、もしコードに不備があるようでしたらご覧いただけると嬉しいです。
174
+
175
+
176
+
177
+ - [htaccessに書き込む処理](https://github.com/chef4536/4536/blob/dev/4536-setting/htaccess-setting.php)
178
+
179
+ - [htaccessの中身を生成する処理](https://github.com/chef4536/4536/blob/dev/htaccess-text/redirect-post-in-category.php)